-
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
Key that must exist on generic type T cannot be used to index type type T #51161
Comments
Some things to note about this. If you remove The other odd thing about this is that the // should be never
type wrongKey = AnyOneofKind<{
oneofKind: 'foo',
bar: 1, // wrong key
}>;
// should be "foo"
type foo = AnyOneofKind<{
oneofKind: 'foo',
foo: 1,
}>;
// should be "foo" | "bar"
type foobar = AnyOneofKind<{
oneofKind: 'foo',
foo: 1,
} | {
oneofKind: 'bar',
bar: 1,
}>; |
You can write: type AnyOneofKind<T extends AnyOneof> = T extends { oneofKind: string & keyof T }
? T['oneofKind']
: never; |
Thanks, that certainly fixes the error. I still don't understand why since the |
I'm not certain, but I believe the issue is that |
Right, but type PossibleValuesForOneofKindProperty = string | undefined;
type KeyofT = string | symbol;
type JustString = PossibleValuesForOneofKindProperty & KeyofT; // string |
The change between release-4.5 and main occurred at fc82c67. |
PR for that commit is #46812 |
An equivalent type, as far as I can tell, is export type AnyOneofKind<T extends AnyOneof> = T extends unknown ? keyof T & T['oneofKind'] : T
It happens in this case that the definition of If someone is able to fix this without regressing other cases, that's fine, but this is Backlog milestone unless there are definitions without an available rewrite. |
FYI @weswigham in case it turns out there's a trivial fix you can think of |
Bug Report
🔎 Search Terms
"Cannot be used to index", "generic conditional", "generic extends ternary branch"
Seems similar to #50462 and #50465 but both of those tickets have since been fixed in 4.8.1 and 4.9 respectively while my example is still broken in the playground using 4.8 or nightly (which as of writing this is v4.9.0-dev.20221013)
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In TS versions greater than v4.6.4 (I've only tested versions available in the playground) the
T['oneofKind']
on line 3 is highlighted as an error:Type '"oneofKind"' cannot be used to index type 'T'.(2536)
.🙂 Expected behavior
No error on line 3 as
"oneofKind"
can in fact be used to index into typeT
sinceT
must have property"oneofKind"
.The text was updated successfully, but these errors were encountered: