Created
September 15, 2020 11:38
-
-
Save hmmhmmhm/62bde54d64feb507150a31ff12c95cdd to your computer and use it in GitHub Desktop.
타입스크립트로 인터페이스 속성에서 꼭 하나는 주어져야하는, 또는 꼭 하나만 주어져야하는 타입 만드는 방법
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
| interface ITestObject { | |
| a: string | |
| b: number | |
| c: Function | |
| d: string[] | |
| } | |
| type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = | |
| Pick<T, Exclude<keyof T, Keys>> | |
| & { | |
| [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> | |
| }[Keys] | |
| type RequireOnlyOne<T, Keys extends keyof T = keyof T> = | |
| Pick<T, Exclude<keyof T, Keys>> | |
| & { | |
| [K in Keys]-?: | |
| Required<Pick<T, K>> | |
| & Partial<Record<Exclude<Keys, K>, undefined>> | |
| }[Keys] | |
| let needToBeLeastOne: RequireAtLeastOne<ITestObject> = {a: ''} | |
| let needToBeOnlyOne: RequireOnlyOne<ITestObject> = {a: ''} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
typescript object conditional optional property type