-
Notifications
You must be signed in to change notification settings - Fork 12.8k
type infer failed with a pathological case in 3.5.0, but in 3.4.0 it works #31385
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
Comments
I think this may have changed under #30769, in particular: string indexers are ignored in type variable constraints. |
@jack-williams |
So the PR I link is the source of the change, but my initial point about string indexers was likely wrong. Corrections (which are probably still wrong in some form, you'll probably need someone on the team to look over this): I think the change of
A reason why there should be an error: In declare const sym: unique symbol;
type T = { x: number, b: string };
type Q = { a: "x", [sym]: number };
type A = Baz<T,Q> // Proof Q is assignable to Foo<T>
type ShouldBeKeyofT<X extends keyof T> = X
type Err = ShouldBeKeyofT<Q[keyof Q]> // err! The new error in Type 'Q[keyof Q]["0"]' is not assignable to type 'keyof T'.
Type 'Q[string]["0"] | Q[number]["0"] | Q[symbol]["0"]' is not assignable to type 'keyof T'.
Type 'Q[string]["0"]' is not assignable to type 'keyof T'.
Type '{ [key_ in keyof T]: [key_]; }[keyof T]["0"]' is not assignable to type 'keyof T'.
Type 'string | number | symbol' is not assignable to type 'keyof T'.
Type 'string' is not assignable to type 'keyof T'. [2322] So it seems like the new logic may be eagerly applying |
I can't represent that error in both 3.4.3 and 3.5.0-dev.20190509.
It can be simply fixed like this, and I think it's an old behavior and has nothing to do with the change. type Err = ShouldBeKeyofT<Q[string & keyof Q]> Could you please share me the playground link for your example? |
I'm running off master (merged just now). I used this snippet to get the extended error message by assigning to function foo<T, Q extends Bar<T>>() {
const a: Q[keyof Q]["0"] = undefined as any;
let b: keyof T;
b = a;
} |
type X<T> = { [key_ in keyof T]: [key_] }[keyof T];
function foo<T>() {
const a: X<T>["0"] = undefined as any;
let b:keyof T;
b = a; // error
}
type Y<T> = { [key_ in keyof T]: key_ }[keyof T];
function bar<T>() {
const a: Y<T> = undefined as any;
let b:keyof T;
b = a; // ok
} I saw the same error in 3.5.0-dev.20190515. And there's no error in 3.4.3. |
Both look like they should be ok to me (so the first seems buggy: it's turning key_ into its constraint), though they are different to your original example. In your original example you are going through generic constraint with a string indexer, here you directly interact with a generic mapped type. The issue in your first example is that only the |
type Baz<T, Q extends Foo<T>> = {
[key in string & keyof Q]: T[Q[key]]
}
type Qux<T, Q extends Bar<T>> = {
[key in string & keyof Q]: T[Q[key]["0"]]
} Then does it solve the problem? I'm asking if both |
Is this going to be fixed before the release of 3.5.0 or later? |
Since The only difference between |
TypeScript Version: 3.5.0-dev.20190509
Search Terms:
generic, keyof
Code
Expected behavior:
Qux is also a valid generic definition.
Actual behavior:
Type 'Q[key]["0"]' cannot be used to index type 'T'.ts(2536)
Playground Link:
https://www.typescriptlang.org/play/#src=type%20Foo%3CT%3E%20%3D%20%7B%0D%0A%20%20%5Bkey%3A%20string%5D%3A%20%7B%20%5Bkey_%20in%20keyof%20T%5D%3A%20key_%20%7D%5Bkeyof%20T%5D%0D%0A%7D%0D%0Atype%20Bar%3CT%3E%20%3D%20%7B%0D%0A%20%20%5Bkey%3A%20string%5D%3A%20%7B%20%5Bkey_%20in%20keyof%20T%5D%3A%20%5Bkey_%5D%20%7D%5Bkeyof%20T%5D%0D%0A%7D%0D%0A%0D%0Atype%20Baz%3CT%2C%20Q%20extends%20Foo%3CT%3E%3E%20%3D%20%7B%0D%0A%20%20%5Bkey%20in%20keyof%20Q%5D%3A%20T%5BQ%5Bkey%5D%5D%0D%0A%7D%0D%0A%0D%0Atype%20Qux%3CT%2C%20Q%20extends%20Bar%3CT%3E%3E%20%3D%20%7B%0D%0A%20%20%5Bkey%20in%20keyof%20Q%5D%3A%20T%5BQ%5Bkey%5D%5B%220%22%5D%5D%20%2F%2F%20error!%0D%0A%7D
Related Issues:
The text was updated successfully, but these errors were encountered: