Earlier quoted context omitted.
Nope. This is a common confusion. The set of properties of objects and the sets of objects themselves have a complementary relationship when it comes to union / intersection and subset / superset. Let's define a "property" as being a predicate that is true for all elements of a set. For example, a collection of red objects has the "red" property. A subset of a set of objects can only have the same or more properties…
maybe a dumb question, but why does wikipedia say typescript is a superset of javascript? https://en.wikipedia.org/wiki/TypeScript
Making sense of TypeScript using set theory
31–40 of 94 posts
Re: Making sense of TypeScript using set theory
#32Earlier quoted context omitted.
Nope. This is a common confusion. The set of properties of objects and the sets of objects themselves have a complementary relationship when it comes to union / intersection and subset / superset. Let's define a "property" as being a predicate that is true for all elements of a set. For example, a collection of red objects has the "red" property. A subset of a set of objects can only have the same or more properties…
maybe a dumb question, but why does wikipedia say typescript is a superset of javascript? https://en.wikipedia.org/wiki/TypeScript
Re: Making sense of TypeScript using set theory
#33Not really a paradox.
- `unknown` is the intersection (&) of all types, including an internal one that has no shape or characteristics. `unknown | {int: number}` should be `{int: number}`. `unknown & T` is always `unknown`, any characteristic T has will be discarded by the intersection.
- `any` is the union (|) of all types, including that internal one that can be anything, and yes, `unknown` too. `(any & {int: number})` should be `{int: number}`. also, `const x: any = 5 as unknown`. Union with `any` should always produce `any`.
Re: Making sense of TypeScript using set theory
#34The 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
#35Not entirely related to the post, but what app did you use to create your diagrams? They look fantastic.
Re: Making sense of TypeScript using set theory
#36> unknown is the set of all JS values. any is a paradoxical set that includes everything, but might also be empty. Not really a paradox. - `unknown` is the intersection (&) of all types, including an internal one that has no shape or characteristics. `unknown | {int: number}` should be `{int: number}`. `unknown & T` is always `unknown`, any characteristic T has will be discarded by the intersection. - `any` is the un…
The intersection of all types is never, because, say, number and string don't intersect. The union of all types is unknown.
any doesn't show set-like properties and yields ternary logic in clauses like any extends T, which makes it a paradox.
Re: Making sense of TypeScript using set theory
#37Great article! Not entirely related to the post, but what app did you use to create your diagrams? They look fantastic.
I use excalidraw: https://excalidraw.com/
I think most web-dev bloggers use it, makes me feel very boring
Re: Making sense of TypeScript using set theory
#38> unknown is the set of all JS values. any is a paradoxical set that includes everything, but might also be empty. Not really a paradox. - `unknown` is the intersection (&) of all types, including an internal one that has no shape or characteristics. `unknown | {int: number}` should be `{int: number}`. `unknown & T` is always `unknown`, any characteristic T has will be discarded by the intersection. - `any` is the un…
‘Any’ does not really fit into a set theoretical model because it can be assigned to anything and anything can be assigned to it, so it is both a supertype of anything and a subtype of anything. Basically it just disables type checking.
Re: Making sense of TypeScript using set theory
#39Earlier quoted context omitted.
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
This is technically correct, but I suspect it's pretty rare that you would choose "unknown" over a generic parameter. Even if you're writing an algorithm that doesn't care about the data type, it's still probably going to be used by application code that does care, and you'll want to support that need by passing the type through, otherwise the application will be forced to downcast.
type Output = Fn extends ((a: unknown) => infer Out) ? Out : never;
Re: Making sense of TypeScript using set theory
#40> 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?
Yeah, at first I was mislead by this.
Initially I thought never is opposite of any while, it turns out, it's opposite of unknown. And any is just super weird, dynamic wildcard thing that is as large or as small as needed for any given operation (but not as small as never).