-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal for proper top type #23838
Comments
Difference between this and #10715 ? |
I'll have a read through, cursory scanning seems to indicate it's a similar concept, but not quite the same. My
Here's an example of why type Test<T> = T extends infer U
? U extends number ? true : false
: never;
type ThisShouldBeFalse = Test<number | {}>; // But it's 'boolean' instead I've run into this peculiarity more than once and it takes quite a bit of digging to figure out the distributive properties of conditionals interact with a union of |
Okay, after reading through the other proposal, it seems like it's a case of and-and. The other proposal assumes something top-like exists and then specifies how it acts under flow-checking. In other words, this proposal makes the other one possible. |
In your examples does
what if |
For the excluded cases and how they interact with each other, it's not always clear. In some cases it is, but I refer you to things like The specific case you mention is simple, because it has to match what |
Ah ok, under that definition of Does Ryan's comment here not include all the properties you want for For the property |
To a very small extent. I'm currently using Excluding the null issue, |
It seems like Ryan's comment does indeed include all properties. It's not in the proposal and it's very far down the page, so I didn't see that post yet. |
I'll summarize my thoughts, cross-post them to the other issue and close this one. If anyone disagrees, I'll reopen this one. |
Right now there are three interesting types for unknown values:
never
can be assigned to anything, but nothing can be assigned to itany
can be assigned to anything and anything can be assigned to itunknown
can't be assigned to anything, but everything can be assigned to itIn type theory terms,
never
is a bottom,unknown
is a top andany
is something you don't usually see, but is really handy for a language like TS.Here are the properties for
never
andany
, aside from the abovementioned ones:(
A
ranges over all types, includingnull
andundefined
, but excludingnever
,any
,{}
andunknown
itself)A | never = A
A & never = never
never extends A ? true : false = true
A | any = any
A & any = any
any extends A ? true : false = true | false
Now, I've been using
type unknown = {} | null | undefined
as my top, like most people. It works mostly fine for assignment, but it fails when it comes to unions, intersections and conditionals. For a proper top type, I need the following properties:A | unknown = unknown
A & unknown = A
unknown extends A ? true : false = false
Can we get a type in the language called
unknown
that fits these properties? Another name that might work would beall
, but I think that's confusing, as the properties it has would confuse people (it's not immediately clear whyA & all = A
in that case.It's become clear to me as of late, I shouldn't use
any
in my code at all, except when I'm absolutely sure I want to turn off type checking. The problem is, a lot of the cases where I useany
, it's because I need a top type. Especially intersections are hard to deal with, without a top type.The text was updated successfully, but these errors were encountered: