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
interfaceMyIFace{key1: stringkey2: number}constMyObject: {[KinkeyofMyIFace]: string}={key1: 'foo',key2: 'bar'}declarefunctionsomeFunction(key: keyofMyIFace): void// error: Argument of type 'string' is not assignable to parameter of type '"key1" | "key2"'Object.keys(MyObject).forEach(key=>someFunction(key))// error: Type 'string' is not assignable to type '"key1" | "key2"'Object.keys(MyObject).forEach((key: keyofMyIFace)=>someFunction(key))Object.keys(MyObject).forEach((key: keyoftypeofMyObject)=>someFunction(key))
Expected behavior:
Object.keys(MyObject) should type it's return as (keyof typeof MyObject)[].
Actual behavior:
Object.keys(MyObject) types it's return as string[].
This can be easily fixed by defining Object.keys as follow :
declareconstBetterObject: {keys<Textends{}>(object: T): (keyofT)[]}// ORdeclareconstBetterObject: {keys(object: {}): (keyoftypeofobject)[]}// everything works and key is infered as "key1" | "key2"BetterObject.keys(MyObject).forEach(key=>someFunction(key))BetterObject.keys(MyObject).forEach((key: keyofMyIFace)=>someFunction(key))BetterObject.keys(MyObject).forEach((key: keyoftypeofMyObject)=>someFunction(key))
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.6.2
Code
Expected behavior:
Object.keys(MyObject)
should type it's return as(keyof typeof MyObject)[]
.Actual behavior:
Object.keys(MyObject)
types it's return asstring[]
.This can be easily fixed by defining
Object.keys
as follow :The text was updated successfully, but these errors were encountered: