Lesson 9 of 10
as const and satisfies
- 13 min
- 5 checks
- 3 hints
- Go to task
Goal
Check the shape without widening the type
Explanation
When you write the type out explicitly, as in const API: Record<string, Endpoint>, the compiler checks the shape but forgets what it knew about the value. The key type widens to plain string, so describe("dropTable") passes without complaint and then fails at runtime. Every method also becomes just a Method instead of a specific "POST".
as const asks for the narrowest type possible: every string keeps its literal value and every property becomes readonly. satisfies checks the shape without changing the type, so a mistake in any entry shows up right where you define it, and the names and values stay exactly as you wrote them. Then keyof typeof API takes the object's type and pulls out its keys.
Order matters here: { ... } as const satisfies Record<string, Endpoint>. The reverse is a compile error, because as const only applies to a value written out literally in the code.
Your task
Remove the type written after API and add as const satisfies Record<string, Endpoint> after the object. EndpointName is then just the names of the three entries in API, each method keeps its literal value, and the entries can't be changed.
Checks
5 checks- No type errors
- The describe function still works
- A name that doesn't exist causes a compile error
- The method property keeps its literal value
- The entries of API are read-only