TypeScript · Middle · Question 47 of 75

When does TS widen a literal type to a general one?

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.

Further reading

Problem with a question

TypeScript · Middle · question 47

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz