TypeScript · Middle · Question 47 of 75
When does TS widen a literal type to a general one?
- ✓Correct answer: In mutable positions:
let, object properties - 2Never: literal types are always preserved as is
- 3Only with the
noImplicitAnyflag enabled in tsconfig
Explanation
const a = 'x' → 'x', but let b = 'x' → string, and { dir: 'left' } → { dir: string }. Because of this, the object can't be passed to a function expecting 'left' | 'right'. Fixes: as const, an annotation or satisfies.