#how can I do Object.keys as const
1 messages · Page 1 of 1 (latest)
no, as const is for literals
separately, Object.keys can't be specific because:
!:unsafe-keys
`!retsam19:unsafe-keys`:
Since TS allows objects to have extra properties not specified in the type, it doesn't assume that all the keys on the type are the only keys on the object. This means that Object.keys returns string[] not a specific type, and for(const key in obj), key is string, (not keyof typeof obj).
If you wish to assume otherwise, this utility is often helpful:
// A signature for `Object.keys` that assumes the only keys are the ones indicated by the type
const unsafeKeys = Object.keys as <T>(obj: T) => Array<keyof T>;