3 ms·
Defining `MyType = 3 | "foo" | false` needs the language to allow arbitrary type changes of a variable, and while that's possible in JS it's generally not consi
by alpaca128 4y ago
Defining `MyType = 3 | "foo" | false` needs the language to allow arbitrary type changes of a variable, and while that's possible in JS it's generally not considered a good idea.
Rust forces you to define a single enum to represent this, and then you can implement the TryFrom trait to make it directly comparable to booleans, integers etc, but without having to deal with random `undefined` values popping up everywhere.
Is it less convenient? Yes, but so are seatbelts. Sometimes you need to build software where this tradeoff is worth it.
- dpratt71 4y agoIn your example, '3 | "foo" | false' is the type of the variable. What is the argument for this being a bad idea in general? You imply it's unsafe. How?
- alpaca128 4y ago> In your example, '3 | "foo" | false' is the type of the variable Depends. TypeScript would (usually) recognize 3 being a part of that union type, but it's still a normal integer that is fully compatible with other numbers. If you add 1 to it the variable is still a number but doesn't belong to this union type anymore. You could of course argue that this is just a consequence of TS being wrapped around JS, but whether the type changes between `3` and `false` or between `3` and `4`, it does change. In Rust this is not allowed, hence you cannot just do an addition between e.g. an enum variant and a number unless you explicitly implement the interface to make them compatible, in which case the addition produces a new value with - again - an immutable type. If 3, "foo" and false have different types by themselves then the language has to either allow changing the type of a variable or forbid a mix of them in one type.
- dpratt71 4y agoRust is more restrictive than it needs to be, in this case. What matters is whether it's possible to have a situation where the value of a variable or parameter is not concordant with its type in a given context and what it takes to make that happen. Of course it's possible to tell the TS type checker to sod off in a given context (cast, use the "any" type, etc.), but without doing that, I think it's hard to contrive a situation such as I described above. https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiAq5oF4oGYoB8oCIBmA9gTllHgIYA2AzhANwBQDeArgHYDGwAlgW2VAAUADygAuWAiQBKBgG8GUKAHplUACYEo1YCzx4AdEYYBfJpQjAooiXESRGDUajSO8I6Y9EBqVAEY3DzogA https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiAq5oF4...
- zarzavat 4y agoTypeScript lets you fit your types to your data, whatever that data is. If your data includes mixed types, then you can represent those. If I have a JSON array that mixes numbers and strings then I can write (number|string)[]. Most other statically typed languages force you to fit your data to your types. Many statically typed languages simply degenerate into dynamic typing when trying to consume external data, e.g. by representing JSON as an enumeration of every possible JSON value type.