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
In the example below, the calls to getPropertyX(obj, 'first') should bind the type of K as 'first'. However, when the definition of K involves Extract, the inferred binding is 'first' | 'second' instead (i.e. all possible keys).
Apropos due to the keyof changes in 2.9.
Code
// Declaration uses keyof -- no problemsfunctiongetProperty<T,KextendskeyofT>(obj: T,key: K){returnobj[key];}// Type argument uses bare keyof, function argument uses Extract (doesn't work)functiongetProperty2<T,KextendskeyofT>(obj: T,key: Extract<K,string>): T[K]{returnobj[key];}// Type argument uses Extract (doesn't work)functiongetProperty3<T,KextendsExtract<keyofT,string>>(obj: T,key: K): T[K]{returnobj[key];}interfaceStrNumPair{first: string,second: number,}constobj: StrNumPair={}asany;letprop: string;// Worksprop=getProperty(obj,'first');// Doesn't work -- string | number is not assignable to stringprop=getProperty2(obj,'first');// Doesn't work -- string | number is not assignable to stringprop=getProperty3(obj,'first');// Explicit generic binding -- worksprop=getProperty2<StrNumPair,'first'>(obj,'first');prop=getProperty3<StrNumPair,'first'>(obj,'first');
Expected behavior: K is bound to just the key passed ('first')
Actual behavior: K is bound to all possible keys ('first' | 'second').
TypeScript Version: 2.9.2
Search Terms: keyof generic interference
In the example below, the calls to
getPropertyX(obj, 'first')
should bind the type of K as'first'
. However, when the definition of K involvesExtract
, the inferred binding is'first' | 'second'
instead (i.e. all possible keys).Apropos due to the
keyof
changes in 2.9.Code
Expected behavior: K is bound to just the key passed ('first')
Actual behavior: K is bound to all possible keys ('first' | 'second').
Playground Link: http://www.typescriptlang.org/play/#src=%0D%0Afunction%20getProperty%3CT%2C%20K%20extends%20keyof%20T%3E(obj%3A%20T%2C%20key%3A%20K)%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20getProperty2%3CT%2C%20K%20extends%20keyof%20T%3E(obj%3A%20T%2C%20key%3A%20Extract%3CK%2C%20string%3E)%3A%20T%5BK%5D%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20getProperty3%3CT%2C%20K%20extends%20Extract%3Ckeyof%20T%2C%20string%3E%3E(obj%3A%20T%2C%20key%3A%20K)%3A%20T%5BK%5D%20%7B%0D%0A%20%20%20%20return%20obj%5Bkey%5D%3B%0D%0A%7D%0D%0A%0D%0A%0D%0Ainterface%20StrNumPair%20%7B%0D%0A%20%20%20%20first%3A%20string%2C%0D%0A%20%20%20%20second%3A%20number%2C%0D%0A%7D%0D%0Aconst%20obj%3A%20StrNumPair%20%3D%20%7B%7D%20as%20any%3B%0D%0A%0D%0Alet%20prop%3A%20string%3B%0D%0A%0D%0A%2F%2F%20Works%0D%0Aprop%20%3D%20getProperty(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Doesn't%20work%20--%20string%20%7C%20number%20is%20not%20assignable%20to%20string%0D%0Aprop%20%3D%20getProperty2(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Doesn't%20work%20--%20string%20%7C%20number%20is%20not%20assignable%20to%20string%0D%0Aprop%20%3D%20getProperty3(obj%2C%20'first')%3B%0D%0A%0D%0A%2F%2F%20Explicit%20generic%20binding%20--%20works%0D%0Aprop%20%3D%20getProperty2%3CStrNumPair%2C%20'first'%3E(obj%2C%20'first')%3B%0D%0Aprop%20%3D%20getProperty3%3CStrNumPair%2C%20'first'%3E(obj%2C%20'first')%3B
Related Issues:
The text was updated successfully, but these errors were encountered: