Re:Simpler UI Logic With Finite State Machines
by bcherny on Monday December 23, 2019 @01:04PM
(#21865557)
You can use an `in` check for this, since TypeScript version 2.7 [0].
Here's an example (Playground link: [1]):
import * as React from 'react'
type Props =
| {a: number}
| {b: string}
function MyComponent(props: Props): JSX.Element {
if ('a' in props) {
return <div>a is {props.a}</div>
}
return <div>b is {props.b}</div>
}
let x = <MyComponent a={3} />
let y = <MyComponent b="foo" />
Here's an example (Playground link: [1]):
[0] https://www.typescriptlang.org/docs/handbook/release-notes/t...[1] https://www.typescriptlang.org/play/?jsx=2#code/JYWwDg9gTgLg...