I Can’t Believe This TS Feature Has No Documentation

This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).

TypeScript has an undocumented feature called const generics, which lets you add the `const` keyword before a generic type parameter. This automatically applies `as const` semantics to any value passed into the function, giving you the strictest possible literal type inference without requiring callers to manually append `as const` at every call site. The example demonstrates a generic `first()` function that, without const generics, infers a return type of `number` instead of the specific literal union `1 | 2 | 3`.

1m watch time
1 Comment

Sort: