Given the way K2 integrates with the IDE, I suppos...
# javascript
e
Given the way K2 integrates with the IDE, I suppose via a compiler plugin we could get better support for TS unions? Example:
Copy code
tsFunction(value: string | number)
Could be translated to
Copy code
fun tsFunction(value: Union<String, Int>)
And when being called
Copy code
tsFunction("example")
The plugin could erase the Union type and perform type checking. I'm dreaming too much?
t
Overloads works fine in such cases cc @Sergei Grishchenko
e
True, but then you have stuff like
Copy code
Array<string | number>
This can't be nicely expressed unfortunately, as far as I understand
Array<Any>
is the only solution in Kotlin
t
Array<Comparable>
e
That goes only half the way tho
t
In most cases you can find more strict variant and use it
e
Also, there is a readability issue. If I'm creating externals, I can't communicate cleanly to the user the expected types. Like in the above example a user might understand that all comparables are ok, but that's not true.
A very dumb workaround could be using
Copy code
typealias Union<First, Second> = Any
But still no type checking for parameters.
So that at least it's clear what types are expected.
t
If you need strict array - use more useful in your situation
Dumb workarounds has no big sense :(
• Overloads for functions • Interfaces for opaque aliases • Interfaces with assign operators for properties
e
> Dumb workarounds has no big sense 😞 That's true. However, compare these two signatures, which in TS both return
Thenable<void> | void
The first one is much clearer when attempting to use it.
t
Thenable<void> | void
->
PromiseResult<Void>