You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Selects all the keys from T which have a value of VtypeSelectKeys<T,V>={[KinkeyofT]: T[K]extendsV ? K : never}[keyofT];//================================================================// Using a concrete type worksinterfaceFoo{yes: string;no: number;}letfoo: Foo={yes: "hello",no: 5};// This function can only be called with keys which have a value of stringfunctionlookup<KeyextendsSelectKeys<Foo,string>>(key: Key): string{returnfoo[key];}lookup("yes");lookup("no");// This error is correct//================================================================// Using a generic type T does not workclassBar<T>{privatebar: T;constructor(bar: T){this.bar=bar;}// This method can only be called with keys which have a value of stringlookup<KeyextendsSelectKeys<T,string>>(key: Key): string{// This error is NOT correctreturnthis.bar[key];}}newBar<Foo>(foo).lookup("yes");newBar<Foo>(foo).lookup("no");// This error is correct
Expected behavior:
No error occurs.
Actual behavior:
The following error occurs:
Type 'T[SelectKeys<T, string>]' is not assignable to type 'string'.
Type 'T[T[keyof T] extends string ? keyof T : never]' is not assignable to type 'string'.
Type 'T[keyof T]' is not assignable to type 'string'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'.
Type 'T[string]' is not assignable to type 'string'.
TypeScript Version: 4.1.0-dev.20200920
Search Terms: filter key generic
Code
Expected behavior:
No error occurs.
Actual behavior:
The following error occurs:
Playground Link: [Link]
Related Issues: #28111 #30728 #38646
The text was updated successfully, but these errors were encountered: