4 ms·
Well it’s bolted on for a start so introduces overhead from the get go. Its type system is also severely hamstrung and complicated by JS compatibility. It doesn
by jamil7 2y ago
Well it’s bolted on for a start so introduces overhead from the get go. Its type system is also severely hamstrung and complicated by JS compatibility. It doesn’t really compare to Rust, Swift, OCaml etc.
I do think it’s a huge improvement on JS though.
- josephg 2y agoYou say that, but its type system has some wonderful feature that those other languages lack. In what way is it hamstrung? It seems better than C# to me! For example, C# doesn’t support parameterised enums like typescript and rust do. Once you start using parameterised enums, you seriously can’t go back. It’s a killer feature. But even then, typescript goes further. All of these languages let me make a Color enum with red, green and blue variants. But as far as I know, only typescript will let me write a function which takes a strict subset of those variants. In typescript I can have a function that only accepts red or blue - and passing green (or something that might be green) would be a compilation error. Typescript also lets you make another type with a superset of another type’s variants. Eg type Foo = Color | “yellow”. In rust if I want to change a function’s signature to make one of the parameters optional, that’s a breaking api change since all existing callers need to wrap the parameter in Some(val). But in typescript, I just change the parameter’s type from T to T | null and everything works. Again, in what way is it hamstring? I’ll grant that JavaScript doesn’t have quite the performance of C# (though modern runtimes are pretty impressive!). But typescript itself seems great.
- gardenhedge 2y agoDoes TS have parameterised enums? Can you give an example?
- MrJohz 2y agoIt's not an explicit feature, but a combination of arbitrary records, sum types, and type narrowing: type Animal = | { species: "dog", name: string, catsChased: number } | { species: "cat", name: string } const animal: Animal = ... // Available in all cases console.log(animal.name) if (animal.species === "dog") { // Can access dog attrs here console.log(animal.catsChased) } // ERROR: catsChased only exists for dog and not for cat animal.catsChased
- timeon 2y agoCan TS enums carry values like Option<T> ?
- josephg 2y agoSure! Though the normal way to write Option<T> is simply T | null.
- neonsunset 2y ago> C# doesn’t support parameterised enums like typescript and rust do To be fair, you rarely miss them with pattern matching and records. In a similar vein to a sibling comment: var animal = ... Console.WriteLine(animal switch { Animal.Dog(_, var cats) => $"Chased {cats} cats", Animal.Cat or Animal.Bat => "No cats chased", _ => "Unknown animal" }); abstract record Animal(string Name) { public record Dog(string Name, int CatsChased): Animal(Name); public record Cat(string Name): Animal(Name); public record Bat(string Name): Animal(Name); } There are many libraries to further enhance the experience, provide additional exhaustiveness analysis, etc.