Skip to content

Instantly share code, notes, and snippets.

@Woodz
Created June 30, 2023 07:32
Show Gist options
  • Save Woodz/9b99a9780eaa556c0770066049aba15e to your computer and use it in GitHub Desktop.
Save Woodz/9b99a9780eaa556c0770066049aba15e to your computer and use it in GitHub Desktop.
Experimenting with how to strongly type field renames
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