Class Constructor Code Flow
In 4.0, we use control flow analysis to
infer the potential type of a class property based on
what values are set during the constructor.
// In previous versions of TypeScript, `id` would
have been classed as an `any`.
class UserAccount {
id; // Type is inferred as string | number
constructor(isAdmin: boolean) {
if (isAdmin) {
this.id = "admin";
} else {
this.id = 0;
}
}
}