I'm trying to come up with a typescript type that takes an object and sets all keys of type number to be string. The following doesn't work:
export type ChangeTypeOfKeys<T extends object> = {
[key in keyof T]: key extends number ? string : T[key]
}
const c: ChangeTypeOfKeys<{ a: number, b: boolean }> = {
a: 'ok',
b: false,
}
The resulting type should be { a: string, b: boolean }, but instead I get a TS error:
Type 'string' is not assignable to type 'number'.(2322)
input.tsx(5, 29): The expected type comes from property 'a' which is declared here on type 'ChangeTypeOfKeys<{ a: number; b: boolean; }>'
I think the issue is related to how I'm using key in key in keyof T, since replacing key extends number with another argument works:
export type ChangeTypeOfKeys<T extends object, Replace> = {
[key in keyof T]: Replace extends number ? string : T[key]
}
const c: ChangeTypeOfKeys<{ a: number, b: boolean }, number> = {
a: 'ok',
b: 'ok',
}
Not sure if there are any special properties with key that I'm missing here. TIA!