Skip to content

Instantly share code, notes, and snippets.

@turbod
Last active July 17, 2023 06:45
Show Gist options
  • Save turbod/5cd347d2d9511624a84cf13b7e01bd9e to your computer and use it in GitHub Desktop.
Save turbod/5cd347d2d9511624a84cf13b7e01bd9e to your computer and use it in GitHub Desktop.
Conditional Types
type Props = {
name: string
} & (MaleProps | FemaleProps)
type MaleProps = {
gender: 'male'
salary: number
}
type FemaleProps = {
gender: 'female'
weight: number
}
const Child = (props: Props) {
if (props.gender === 'male') {
console.log(props.salary}
else if (props.gender === 'female') {
console.log(props.gender === 'male')
}
return <h1>child</h1>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment