News for hackers. Stuff that matters.
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" />

[0] https://www.typescriptlang.org/docs/handbook/release-notes/t...

[1] https://www.typescriptlang.org/play/?jsx=2#code/JYWwDg9gTgLg...

Re:Simpler UI Logic With Finite State Machines by danenania on Monday December 23, 2019 @06:37PM (#21868500)
Oh nice! I’ll give that a try, thanks.