Skip to content

Instantly share code, notes, and snippets.

@Minozzzi
Created March 15, 2024 19:22
Show Gist options
  • Save Minozzzi/781b3af5e9660953d3ce256f6d4f7485 to your computer and use it in GitHub Desktop.
Save Minozzzi/781b3af5e9660953d3ce256f6d4f7485 to your computer and use it in GitHub Desktop.
This type accepts only one property
type ExactlyOne<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>
}[keyof T]
type User = {
name: string;
age: number;
}
function updateExactlyOne(user: User, newUser: ExactlyOne<User>) {
}
const user = {
name: 'name',
age: 10
};
updateExactlyOne(user, {
age: 20,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment