Roast the Sweet Potatoes: Preheat your oven to 425°F (220°C). Prepare a large rimmed baking sheet by lining it with parchment paper to catch any drippings. Pierce each sweet potato several times with a fork to allow steam to escape during baking. Place the whole sweet potatoes on the baking sheet and roast until they become tender when gently squeezed, about 45 minutes to 1 hour 15 minutes, depending on size. Remove from oven and let cool slightly.
Prepare the Baking Dish: Reduce oven temperature to 350°F (175°C). Lightly grease a 9-inch square baking dish (or similar size, around 2 quarts) with butter or cooking spray. Set aside.
Make the Sweet Potato Filling: Once the sweet potatoes are cool enough to handle, cut each one in half lengthwise. Using a large spoon, scoop out the soft flesh into a large mixing bowl, discarding the skins. Add melted butter, milk, maple syrup, vanilla extract, nutmeg, and salt. Using a hand mixer, whip everything together until the mixture is smooth and creamy. If you don’t have a mixer, you can mash and stir by hand until well combined. Transfer this filling evenly into the prepared baking dish, smoothing the top with a spatula.
Prepare the Pecan Topping: In a medium bowl, combine the softened butter, coconut or brown sugar, chopped pecans, optional rosemary, cinnamon, and a pinch of salt. Stir thoroughly until the ingredients form an evenly mixed topping.
Assemble and Bake the Casserole: Using a spoon or small fork, distribute dollops of the pecan topping evenly over the sweet potato layer. Place the casserole in the preheated 350°F oven and bake for 30 minutes, or until the pecans are toasted, fragrant, and golden brown, and the filling is hot and bubbly around the edges.
Serve and Store: Remove from the oven and let rest briefly before serving. Store any leftovers in an airtight container in the refrigerator for up to 4 to 5 days.