Making sense of TypeScript using set theory
blog.thoughtspile.tech
Making sense of TypeScript using set theory
1–10 of 94 posts
Re: Making sense of TypeScript using set theory
#2Because 'extends' really means 'is assignable to'.
I feel like most of the questions from the post might be answered fairly easily by using reasoning of 'is assignable to'.
Things assignable to A&B are things assignable to both A and B.
Things assignable to A|B are things assignable to A or to B.
'never' means a type that nothing is assignable to and is assignable to everything (bit weird because it usually doesn't have any values but you can create value of type never with the use of 'as').
'unknown' means a type that everything can be assigned to but it's not assignable to anything (except itself and any).
'any' means a type that everything is assignable to and that is assignable to everything (except never).
Ok. Ok. {} types is bit weird way to denote interfaces. :-)
Re: Making sense of TypeScript using set theory
#3Re: Making sense of TypeScript using set theory
#4> Why does 0 | 1 extends 0 ? true : false evaluate to false? Because 'extends' really means 'is assignable to'. I feel like most of the questions from the post might be answered fairly easily by using reasoning of 'is assignable to'. Things assignable to A&B are things assignable to both A and B. Things assignable to A|B are things assignable to A or to B. 'never' means a type that nothing is assignable to and is ass…
Edit: besides, it's quite unintuitve that "never" is assignable to anything. How can never be something?
Re: Making sense of TypeScript using set theory
#5> Why does 0 | 1 extends 0 ? true : false evaluate to false? Because 'extends' really means 'is assignable to'. I feel like most of the questions from the post might be answered fairly easily by using reasoning of 'is assignable to'. Things assignable to A&B are things assignable to both A and B. Things assignable to A|B are things assignable to A or to B. 'never' means a type that nothing is assignable to and is ass…
Fair enough, "is assignable to" is another synonym for "is subset of". I find it much easier to reason about things I can visualize, like sets, which is why I love my set interpretation. Edit: besides, it's quite unintuitve that "never" is assignable to anything. How can never be something?
Re: Making sense of TypeScript using set theory
#6The difference between `any` and `unknown` is that `any` is an escape hatch from the type system. `any` can be used anywhere and will satisfy any type constraint. `any` is how the developer says to the type system "trust me, I know what I'm doing, don't worry about this particular value." Unknown, on the other hand, is for untyped code from an imported JS library, JSON data received from the network that may or may n…
Re: Making sense of TypeScript using set theory
#7The difference between `any` and `unknown` is that `any` is an escape hatch from the type system. `any` can be used anywhere and will satisfy any type constraint. `any` is how the developer says to the type system "trust me, I know what I'm doing, don't worry about this particular value." Unknown, on the other hand, is for untyped code from an imported JS library, JSON data received from the network that may or may n…
Re: Making sense of TypeScript using set theory
#8The difference between `any` and `unknown` is that `any` is an escape hatch from the type system. `any` can be used anywhere and will satisfy any type constraint. `any` is how the developer says to the type system "trust me, I know what I'm doing, don't worry about this particular value." Unknown, on the other hand, is for untyped code from an imported JS library, JSON data received from the network that may or may n…
I think "unknown" is more than that! Basically it's a way to say you don't care about the type, as in Record or P extends Promise
Re: Making sense of TypeScript using set theory
#9> Subtype of type A is a subset of type A. Supertype is a superset. Easy.
Subtype of type A is actually a superset of type A, since it contains at least all the properties of A.
If you had (contrived example) a Dog class that inherited from an Animal class, Dog would be a subtype of Animal, but its additional properties (say, a bark() method) mean that it actually has a superset of the properties in Animal. And vice versa: Animal is a supertype of Dog, but it's a subset because it contains only the properties that Dog inherits.
Re: Making sense of TypeScript using set theory
#10The difference between `any` and `unknown` is that `any` is an escape hatch from the type system. `any` can be used anywhere and will satisfy any type constraint. `any` is how the developer says to the type system "trust me, I know what I'm doing, don't worry about this particular value." Unknown, on the other hand, is for untyped code from an imported JS library, JSON data received from the network that may or may n…
I think "unknown" is more than that! Basically it's a way to say you don't care about the type, as in Record or P extends Promise
obj.value
Will return a type error that value doesn’t exist on unknown. While calling it on an ant is valid typing