TypeScript Master (50 Qs)•Part 1: Basic & Foundational TypeScript (Junior / Entry-Level)
What are Tuples and `as const` assertions?
junior LevelCode ExampleTypeScript
In Simple Words:
- Tuple: An array with a fixed length and known types at specific indices:
[string, number]. as const(Const Assertion): Tells the compiler to infer the most specific, literal, readonly type possible. Prevents type widening from'admin'tostring.
typescript
// Standard array (widened to string[])
const roles1 = ['admin', 'editor'];
// Tuple with immutable literal types: readonly ['admin', 'editor']
const roles2 = ['admin', 'editor'] as const;