1. Pattern matching switch against types(like Scala, Haskell, Ocaml)
2. Input validation based on the already defined typescript interfaces
1. Pattern matching switch against types(like Scala, Haskell, Ocaml)
2. Input validation based on the already defined typescript interfaces
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.
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.It’s easily the #1 thing I miss syntax-wise after using Elixir/Erlang for any period of time.
Patterns fit perfectly with the typed and functional approach, especially with Maybe monads which are used everywhere in some apps.
This makes it additionally useful for scenarios where we are writing a typescript library but also want good descriptive error messages for users who use vanilla javascript.
Even for type-checking at io boundaries [1], it is more useful than jsonschema because using io-ts we can, besides validating the data, also transform our incoming and outgoing data into richer data-structures (eg. string <-> date instance, plain javascript objects <-> class instances) through use of encoders and decoders.
[1] https://lorefnon.tech/2018/03/25/typescript-and-validations-...