4 ms·
My wishlist for Typescript is: 1. Pattern matching switch against types(like Scala, Haskell, Ocaml) 2. Input validation based on the already defined typescrip
by noncoml 7y ago
My wishlist for Typescript is:
1. Pattern matching switch against types(like Scala, Haskell, Ocaml)
2. Input validation based on the already defined typescript interfaces
- rictic 7y ago1. Use discriminated union types: https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions 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 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 https://github.com/Polymer/tachometer handles config file parsing and it works very well.
- noncoml 7y agoMy only problem with 1. is that it desn’t work that well with Error
- azangru 7y agoAlso, (1) won’t be exhaustive.
- alangpierce 7y agoThere 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.
- dmix 7y agoAny who builds a new language and doesn’t make pattern matching a big part of the language is seriously missing out. 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.
- spankalee 7y agoTypeScript isn't really a new language. It's a (mostly) type-checking-only extension to JavaScript. The team has made a policy of not adding any more runtime behavior changing features. JavaScript will need to add pattern matching for TypeScript to get it.
- dmix 7y agoOh I know, I actually don't expect it from Typescript. It's portability with JS code is TS's best feature and I don't see how you could marry the two (maybe I'm wrong here). I just wanted to share how much I like pattern matching.
- pikamonad 7y agoHave been using io-ts to accomplish the latter. We create io-ts types first and then leverage its TypeOf function to generate the regular TS types.
- lf-non 7y agoThe biggest advantages of io-ts over the jsonschema based type generation approach is that our data doesn't have to be json and we can handle non-serializable and complex data structures eg. class instances etc. 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-at-runtime-boundaries/ https://lorefnon.tech/2018/03/25/typescript-and-validations-...
- jbraithwaite 7y ago2. Check out fp-ts or io-ts for runtime type checking.