We have this thing where we perform union subtype reduction - reducing constituents of unions when a union contains a supertype.
any is not the best type to choose from.--strictSubtypes but with a better name.VS Code has a tool called gulp-tsb
--watch is faster than tsc --watch because it assumes only direct dependencies will be affected.https://github.com/microsoft/TypeScript/pull/35200
isolatedModulestsc --initisolatedModules currently?preserve be default in tsc --inithttps://github.com/microsoft/TypeScript/pull/33587
As a compiler writer, what relationship do I use?
- [[rules that went by too fast, but you can consult with the team]]
It was
I recently convinced myself of why the subtype relation is necessary for narrowing when there are some any properties involved, and I think it鈥檚 a good, understandable example. Consider two types:
interface User {
name: any;
}
interface SpecialUser extends User {
name: string;
age?: number;
}
We intuitively understand SpecialUser to be more specific/narrow than User, but the two types are assignable to each other because of name: any. So imagine narrowing the type of some object like this:
declare let someone: unknown;
if (isUser(someone) && isSpecialUser(someone)) {
someone.age;
}
We could simply use User & SpecialUser, but then a) someone.name would be any and we can do better than that, and b) the intersection is fairly useless in general, and it would be nice not to have to carry it around. So we may as well see if one of these two types is sufficient for someone before falling back to the intersection. If we try to use assignability here, we鈥檙e going to end up picking an arbitrary one, or not getting any useful info, because each is assignable to the other. Assignability is incapable of telling us which type is a better fit. But the subtype relation treats any like a top type, so it can reliably tell that SpecialUser is a subtype of User, but not the other way around. (Playground)
Most helpful comment
It was
I recently convinced myself of why the subtype relation is necessary for narrowing when there are some
anyproperties involved, and I think it鈥檚 a good, understandable example. Consider two types:We intuitively understand
SpecialUserto be more specific/narrow thanUser, but the two types are assignable to each other because ofname: any. So imagine narrowing the type of some object like this:We could simply use
User & SpecialUser, but then a)someone.namewould beanyand we can do better than that, and b) the intersection is fairly useless in general, and it would be nice not to have to carry it around. So we may as well see if one of these two types is sufficient forsomeonebefore falling back to the intersection. If we try to use assignability here, we鈥檙e going to end up picking an arbitrary one, or not getting any useful info, because each is assignable to the other. Assignability is incapable of telling us which type is a better fit. But the subtype relation treatsanylike a top type, so it can reliably tell thatSpecialUseris a subtype ofUser, but not the other way around. (Playground)