Created
March 15, 2024 19:22
-
-
Save Minozzzi/781b3af5e9660953d3ce256f6d4f7485 to your computer and use it in GitHub Desktop.
This type accepts only one property
This file contains hidden or 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
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