Assertion
Type assertions helps you to force types when you are not in control of them. For e.g. -
- you are processing user data (with unreliable types)
- working with data that has changed shape over years (employee code was numeric, now it is alphanumeric :))
- receiving data from an external program
Type assertions let the Typescript compiler know that a given variable should be treated as belonging to a certain type. There are no “exceptions” or data restructuring associated with assertions, except minimal validations (we refer this behaviour as “validations that are applied statically”).
There are two ways to do type assertions.
- Bracket syntax. e.g.
let length: number = (<string>lengthField);
- Use as. e.g.
let length: number = lengthField as string;
There is no difference between the two ways and the end result is always the same. Note that if you are using JSX you have to useas
syntax.
Cook
Assert as a subtype on an Interface/Type
interface Data {
events: {
id: string;
}[];
}
const copiedEvents = [...data.events] as Data['events']