Bring a large pot of water to a rolling boil and season generously with salt, then add the spaghetti and cook until al dente according to package directions, stirring occasionally.
While the pasta cooks, place the sliced garlic and olive oil in a cold large skillet, then set over medium-low heat and warm gently until the garlic softens and just begins to turn light golden, about 6–7 minutes; do not let it burn.
When the garlic is nearly done, add 1/2 cup of reserved pasta cooking water to the skillet to stop the garlic from cooking further and loosen the oil.
Drain the spaghetti, reserving more pasta water, and add the pasta directly to the skillet with the garlic oil.
Add the chopped parsley, optional basil, and grated Parmesan to the pasta and toss vigorously to combine, adding up to an additional 1/4 cup of pasta water if the sauce seems dry, until the pasta is evenly coated.
Serve immediately, and optionally finish with extra Parmesan, a pinch of red pepper flakes, or freshly ground black pepper to taste.