You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's look at that f("42", 42) line. It says compiler can't infer type because the type here would be f[int | string] and Go doesn't allow union to be used that way. You can't use union anywhere but inside interface that is used as a type constraint.
Go approach
Makes impossible situations like string + int. Go is a strong-typed language which means you must explicitly cast one type into another in such situations. Go does have operators that are builtin functions with predefined syntax. You won't find type declarations for them like you can for builtin functions like make or new but their behavior is described in the spec. Operator + e.g. works for both numeric types and strings but both arguments must be of the same type.
TS approach
On the other hand TypeScript is a superset of JS which is a weakly-typed language. You can sum numbers with strings and much more. That's why using unions everywhere makes sense. However, such implicit type casts works not everywhere. E.g. you can't - from string to number. This leads to situations like this:
You can probably do function f<T number | string>(a, b T) which shouldn't change anything here.
The text was updated successfully, but these errors were encountered:
emil14
changed the title
Unions - only contraints (like go) or everywhere (like ts)?
Unions - only as constraints (like go) or everywhere (like ts)?
Dec 17, 2022
At the lowest level message is passed to an operator. Operator can have type int|str. What will happen if we'll have messages of different types? Or with the same?
Consider this valid TS code:
It's impossible to do so in Go because
Let's look at that
f("42", 42)
line. It says compiler can't infer type because the type here would bef[int | string]
and Go doesn't allow union to be used that way. You can't use union anywhere but inside interface that is used as a type constraint.Go approach
Makes impossible situations like
string + int
. Go is a strong-typed language which means you must explicitly cast one type into another in such situations. Go does have operators that are builtin functions with predefined syntax. You won't find type declarations for them like you can for builtin functions likemake
ornew
but their behavior is described in the spec. Operator+
e.g. works for both numeric types and strings but both arguments must be of the same type.TS approach
On the other hand TypeScript is a superset of JS which is a weakly-typed language. You can sum numbers with strings and much more. That's why using unions everywhere makes sense. However, such implicit type casts works not everywhere. E.g. you can't
-
fromstring
tonumber
. This leads to situations like this:You can probably do
function f<T number | string>(a, b T)
which shouldn't change anything here.The text was updated successfully, but these errors were encountered: