Created
June 30, 2023 07:32
-
-
Save Woodz/9b99a9780eaa556c0770066049aba15e to your computer and use it in GitHub Desktop.
Experimenting with how to strongly type field renames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Foo { | |
a: string; | |
b: number; | |
c: { | |
a: boolean[]; | |
} | |
} | |
type RenameAToZ<T> = T extends { a: any } ? { | |
[K in keyof Omit<T, 'a'>]: RenameAToZ<T[K]>; | |
} & { | |
z: T['a'] | |
} : { | |
[K in keyof Omit<T, 'a'>]: RenameAToZ<T[K]> | |
}; | |
type FooZ = RenameAToZ<Foo>; | |
const z: FooZ = { | |
z: 'foo', | |
b: 1, | |
c: { | |
z: [true] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment