Bake the crust. Prebake a 9-inch pie shell according to package or recipe instructions.
Let it cool completely so it stays crisp under the filling.
Whisk the dry ingredients. In a medium saucepan (off heat), whisk sugar, cocoa, cornstarch, and salt until no lumps remain. Mixing dry first helps prevent clumps later.
Add milk gradually. Whisk in a splash of milk to make a smooth paste, then slowly add the rest, whisking constantly to keep it silky.
Cook the custard. Set the pan over medium heat. Stir steadily with a whisk or heatproof spatula, scraping the bottom and corners.
You’ll see steam, then small bubbles and thickening after 6–10 minutes.
Temper the yolks. In a small bowl, whisk the egg yolks. Ladle in a bit of the hot mixture while whisking to warm them, then pour the yolks back into the saucepan. Cook 1–2 minutes more until thick and glossy.
Finish with butter and vanilla. Remove from heat.
Stir in butter and vanilla until smooth. If using chopped chocolate, add it now for a deeper flavor and extra sheen.
Strain for perfection. For the smoothest filling, pour the custard through a fine-mesh sieve into a bowl. This catches any tiny cooked egg bits or lumps.
Fill the crust. Scrape the warm custard into the cooled crust and smooth the top.
Press a piece of plastic wrap directly on the surface to prevent a skin if you’re not adding meringue.
Chill. Refrigerate at least 4 hours, or until fully set and sliceable. Overnight is even better for clean slices.
Top it off. For whipped cream: Beat the cream, powdered sugar, and vanilla to soft peaks and spread over the cold pie. For meringue: Beat whites with cream of tartar to soft peaks, gradually add sugar to stiff glossy peaks, spread over warm filling, seal edges to crust, and bake at 350°F (175°C) for 12–15 minutes until golden.
Cool completely before slicing.
Serve. Use a sharp knife dipped in warm water for neat cuts. Wipe the knife between slices to keep edges clean.