News for hackers. Stuff that matters.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by rictic on Thursday September 12, 2019 @06:07PM (#20956975)
1. Use discriminated union types: https://www.typescriptlang.org/docs/handbook/advanced-types.... - TypeScript these days sticks with the runtime semantics of JavaScript, and discriminated unions are a very natural way to express algebraic data types in JS.

2. Use https://github.com/YousefED/typescript-json-schema to generate a JSON schema, then use a json schema validator to do runtime validation. That's how https://github.com/Polymer/tachometer handles config file parsing and it works very well.

Re:TypeScript 3.7: The Biggest Features and How to Use Them by noncoml on Thursday September 12, 2019 @06:13PM (#20957023)
My only problem with 1. is that it desn’t work that well with Error
Re:TypeScript 3.7: The Biggest Features and How to Use Them by azangru on Thursday September 12, 2019 @06:24PM (#20957116)
Also, (1) won’t be exhaustive.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by alangpierce on Thursday September 12, 2019 @07:34PM (#20957626)
There are a few ways to get exhaustiveness checking in TypeScript when using tagged unions. The way I usually do it is an assertNever function like this:

    function assertNever(value: never): any {
      return value;
    }
Then if you call assertNever on your variable in the default switch case or the last else block, TypeScript will error if you didn't handle every case, since it expects the variable to have been narrowed to the "never" type since there's no remaining value that it can take on. That's also a good place to do a runtime check since TS types aren't guaranteed to match runtime values.