News for hackers. Stuff that matters.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by noncoml on Thursday September 12, 2019 @05:59PM (#20956915)
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

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.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by dmix on Thursday September 12, 2019 @06:12PM (#20957011)
Any 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.

Re:TypeScript 3.7: The Biggest Features and How to Use Them by spankalee on Thursday September 12, 2019 @06:50PM (#20957304)
TypeScript 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.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by dmix on Thursday September 12, 2019 @07:31PM (#20957611)
Oh 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.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by pikamonad on Thursday September 12, 2019 @06:12PM (#20957016)
Have 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.
Re:TypeScript 3.7: The Biggest Features and How to Use Them by lf-non on Thursday September 12, 2019 @10:07PM (#20958493)
The 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-...

Re:TypeScript 3.7: The Biggest Features and How to Use Them by jbraithwaite on Thursday September 12, 2019 @06:16PM (#20957062)
2. Check out fp-ts or io-ts for runtime type checking.