TypeScript Version: 2.8.0-dev.20180216
Search Terms:
Code
// Behaviour with string keystypeStringKeys1={a: any,b: any,c: any};// Specific keys onlytypeStringKeys2={[s: string]: any};// Indexer onlytypeStringKeys3={[s: string]: any,foo: any};// Indexer + some keystypeS1=keyofStringKeys1;// S1 = "a" | "b" | "c"typeS2=keyofStringKeys2;// S2 = stringtypeS3=keyofStringKeys3;// S3 = string// Behaviour with numeric keystypeNumberKeys1={1: any,2: any,3: any};// Specific keys onlytypeNumberKeys2={[n: number]: any};// Indexer onlytypeNumberKeys3={[n: number]: any,42: any};// Indexer + some keystypeN1=keyofNumberKeys1;// N1 = "1" | "2" | "3"typeN2=keyofNumberKeys2;// N2 = never <===== should be stringtypeN3=keyofNumberKeys3;// N3 = "42" <===== should be stringExpected behavior:
N2 and N3 should be string.
Actual behavior:
N2 is never and N3 is "42".
Related Issues:
#13715 seems related but isn't really - it is asking for keyof T to allow numbers.
#22042 has an example showing how lodash typings incorrectly produce never for objects that have numeric indexers.
Discussion:
In JavaScript, both string and numeric keys end up effectively being string keys. keyof uniformly handles string keys, numeric keys, and string indexers. Why does it ignore numeric indexers?
TypeScript Version: 2.8.0-dev.20180216
Search Terms:
Code
Expected behavior:
N2andN3should bestring.Actual behavior:
N2isneverandN3is"42".Related Issues:
#13715 seems related but isn't really - it is asking for
keyof Tto allow numbers.#22042 has an example showing how
lodashtypings incorrectly produceneverfor objects that have numeric indexers.Discussion:
In JavaScript, both string and numeric keys end up effectively being string keys.
keyofuniformly handles string keys, numeric keys, and string indexers. Why does it ignore numeric indexers?