Loading topic
const x = "left" the type "left", but let x = "left" the type string. This chapter explains that difference. It covers where widening happens, why the compiler does it, and the errors it causes. The chapters that follow use as const to control widening, so this one explain8 min readlet and const, union and literal types such as "a" | "b", and how to use typeof and keyof in type positions. It builds on the idea of literal widening: TypeScript often replaces a precise literal type like `"left"7 min readas const does to an object literal. You will see how property types change, how the change reaches nested values, and where it stops. It assumes you know the difference between let and const, how literal types such as "a" | "b" work, and why TypeScript widens `let x 7 min readlet and const, and literal types such as "a" | "b". It also assumes you can use typeof and keyof in type positions. The previous chapters covered literal widening and how as const treats object literals. Here we7 min readas const, a value is no longer a plain mutable array or object. This chapter covers what that does to function calls. You will see why a readonly tuple is rejected by some signatures and accepted by others, how to write signatures that accept both, and where TypeScript's readonly checkin7 min readas const widens and narrows literals, how it makes objects readonly and arrays into tuples, and how typeof and indexed access derive union types. It covers where the assertion stops working: which expressions it accepts, what it does at runtime (nothing), and wh6 min readas const does to object and array literals, how to derive a union with (typeof X)[number], and the basics of union and literal types. It also assumes you can read an error message about readonly assignability. You need TypeScript 4.9 or later, because `satisf7 min readenum keyword and a plain object marked as const. It assumes you know the following:8 min readas const at the place where a value is written. This chapter moves the same behavior into a function's signature. With a const type parameter, callers get literal, readonly inference without having to write as const themselves. The feature needs TypeScript 5.0 or later.7 min read