Bring Immutability and Context to Arrays
Const assertions provide immutability but lack semantic meaning. Learn how TypeScript's named tuples and readonly arrays combine to give you both type safety and self-documenting code.
Const assertions provide immutability but lack semantic meaning. Learn how TypeScript's named tuples and readonly arrays combine to give you both type safety and self-documenting code.
An assertion function is a runtime check that identifies the type of unknown input. TypeScript's compiler assumes the input is of the type claimed by the assertion function's signature. Assertion functions are useful for uncertain values, like user input, and generate runtime checks. They can raise errors if the input doesn't meet criteria.
TypeScript 2.1 introduced mapped types, which allow you to create new types based on the properties of an existing type. For example, you can create a mapped type that has the same keys as an existing type, but with optional values.
TypeScript 3.4 introduced const assertions, which allow you to claim a value as immutable. This is useful when working with arrays, as it prevents new values from being added to an existing array.