Created
June 21, 2021 07:48
-
-
Save webflo-dev/538eb190be65ae44add1c77338080470 to your computer and use it in GitHub Desktop.
Conditionnal Props in React/TypeScript
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 DrawerProps = { fullName: string } & ( | |
| { shape: 'circle'; radius: number } | |
| { shape: 'square'; width: number } | |
| { shape: 'rectangle'; width: number; height: number } | |
); |
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 OneOrTheOtherProps = | |
| { collapsed?: true; expanded?: never } | |
| { collapsed?: never; expanded?: true }; |
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 PanelProps = | |
| { collapsable: true; defaultCollpsed?: boolean } | |
//| { collapsable: true; defaultCollpsed?: true } | |
| { collapsable?: never; defaultCollpsed?: never }; |
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 DropdownProps<T> = T extends number | string | |
? { | |
data: Array<string | number>; | |
labelProp?: never; | |
valueProp?: never; | |
} | |
: { | |
data: Array<T>; | |
labelProp: keyof T; | |
valueProp: keyof T; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment