Hacker News new | ask | show | jobs
by brokebook 3273 days ago
this is great news. the code below failed to compile in 2.3 but works in 2.4. it was an annoying bummer when working with ADTs in typescript. It's also nice that you can use string enum for the discriminators now. "{ kind: Kind.Ok, value: 'bla' }" feels less stringly.

  interface Ok<T> { kind: "ok"; value: T; }
  interface Err { kind: "err"; error: string; }
  type Result<T> = Ok<T> | Err

  const maybeNumbers: Result<number>[] = [
    { kind: "ok", value: 10 },
    { kind: "err", error: 'some error' },
    { kind: "ok", value: 9 },
  ];

  function isOk<T>(thing: Result<T>): thing is Ok<T> {
    return thing.kind === "ok";
  }

  const okNumbers: Ok<number>[] = maybeNumbers.filter(isOk);