-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Make T[never] = never instead of erroring or being any #22787
Conversation
@kpdonn maybe clarify that this change only affects types with no index signature. So for example:
With that qualification, this is a non-breaking change because it only affects cases that are currently errors. |
@ahejlsberg and @sandersn any comments? |
After discussing it in person with @ahejlsberg, we didn't like the fact that type Ni = { [n: number]: boolean }
type Kni = keyof Ni // numeric-string
type Nikni = Ni[Kni] // boolean lodash would no longer break because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should wait on this until we have decided what to do about the numeric-string type.
I'm actually okay with taking this PR since it moves us closer to where we eventually want to be, i.e. that That said, we need a small change to the PR. I will review below. |
src/compiler/checker.ts
Outdated
@@ -8131,6 +8131,9 @@ namespace ts { | |||
return anyType; | |||
} | |||
} | |||
if (indexType.flags & TypeFlags.Never) { | |||
return neverType; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move these three lines up and insert then before line 8122. That way an obj[key]
expression where key
is an expression of type never
will produce never
(instead of the any
it produces now).
@ahejlsberg Updated with the requested change and also a test for the Not sure why the CI builds failed though, I ran Edit: Nevermind, looks like rebasing off of latest master resolved the CI build failures. |
And update the baselines for the tests for this change.
And update baseline to reflect new behavior.
All right, it’s in! Thanks @kpdonn! |
This change makes it so that when indexing a type with
never
(e.g.T[never]
), the result isnever
. Previously it was an error to explicitly index a type withnever
and it would non-intuitively becomeany
if it was inferred somewhere inside generics.There is more discussion about the reasoning for the change and the implications of it from @sandersn and @yortus in #22042.
Fixes #22042