-
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
T[keyof T] should be never for T={} #22042
Comments
Note that adding if (indexType.flags & TypeFlags.Never) {
return neverType;
} in |
There is one break in Large Microsoft Project 199 (the one with a blue logo. no, the other one): // NOT THE ACTUAL CODE
import _ = require('lodash');
interface A {
m(): void;
}
const xs = { [n: number]: A } = {};
// other code ...
function all() {
_.each(xs, x => x.m());
}
type A = { p: number }
type It<T, U> = (value: T[keyof T]) => U;
declare function _each<T>(collection: T, iteratee: It<T, any>): void;
function allofem(xs: { [n: number]: A }) {
_each(xs, x => x.p)
} |
@yortus I don't think it's clear whether it's a bug or a suggestion. I'm leaning toward bug, but either way we need to look at the pros and cons of fixing it. Re:rarity. I'm not sure about incredibly rare — lodash had 2.5 million downloads yesterday and Of course we can probably fix lodash to work around any changes that we end up making. The question is whether other code will run into it. |
@sandersn right - actually I see that But isn't this showing up a different problem with Since If |
I opened a new issue at #22105 regarding |
@sandersn everything works if you preserve the special handling of types with string/numeric indexers in If you handle the Actually if done this way, wouldn't it be a strictly non-breaking change, since the only changed behaviour is for cases that are currently compile-time errors? |
@yortus Thanks for the additional investigation. I'm heads down on Javascript work right now, but I'll come back to this when I have time, or if you want to submit the PR, that would work too. |
Heads up I ran into this problem also, and based on everything you guys said it sounded pretty simple to fix so I created a PR at #22787. |
@ahejlsberg have a look at the associated PR? |
TypeScript Version: 2.8.0-dev.20180216
Search Terms:
Code
Expected behavior:
A[keyof A]
is a valid indexed access type that evaluated tonever
.Actual behavior:
A[keyof A]
is treated as an error, and evaluates toany
(presumably due to compiler bailout).Related Issues:
#11929 describes indexed access types as follows:
In this case
K=never
andT={}
, soK
is assignable tokeyof T
, soT[K]
appears to be a valid type.The union of zero property keys is
never
, as tsc shows withkeyof {} = never
.So the result should be the union of zero property types, i.e. also
never
.Consequences:
In more complex generics this leads to meaningful problems, e.g. in #21988 where
RequiredProps<{}>
evalutes to{[x: string]: any}
when it should be{}
.The text was updated successfully, but these errors were encountered: