Preheat the oven to 160C/Gas 4/fan oven 140C. Rub the butter all over the surface of a gratin dish, about 18x28cm/7x11in. Peel and slice the potatoes to a width of 3mm/1⁄8in. Lay the slices on a clean tea towel and pat dry. Keep them covered with the tea towel while you prepare the rest of the ingredients.
Pour the milk and cream into a saucepan. Add the garlic, thyme and shallot (if using). Slowly heat the milk and, just as it is about to reach boiling point and you see bubbles appearing around the edge of the pan, remove it from the heat. Strain the liquid into a large jug, sprinkle in the nutmeg and keep warm.
Layer half the potato slices in the dish, slightly overlapping the slices and sprinkling with a little salt and freshly ground pepper between each layer. You don’t have to be too neat with the lower layers, but keep some of your best slices for later, so the top looks good.
Pour half the hot milk and cream over the potatoes, then finish off layering the rest of the potatoes (arranging them a bit more carefully this time). Pour over the rest of the hot milk and cream. Scatter the cheese over the top and bake for about one hour, until golden and tender. Leave the dish to stand for about 5 minutes, then serve sprinkled with a few fresh thyme leaves.