If I define a method to return an exception, the t...
# javascript
g
If I define a method to return an exception, the typescript exposes the actual implementation, like :
buildIllegalArgumentException(msg: string): kotlin.IllegalArgumentException;
But in VsCode it's handled as
any
, I suspect because the kotlin.XxxException are not exposed in the .d.ts. Is there an option to make them defined/accessible for typescript?
Manually, I was thinking about patching the problem by adding typescript definitions myself
Copy code
export namespace kotlin {
    class Throwable {
        constructor()
        constructor(message: String)
        constructor(message: String, cause: Throwable)
        constructor(cause: Throwable)

        readonly name: string;
        readonly stack: string;
        readonly message: string;
    }

    class Exception extends Throwable{
        constructor()
        constructor(message: String) 
        constructor(message: String, cause: Throwable)
        constructor(cause: Throwable)
    }
    [...]
it works for name/stack/message but not enough to use instanceof (
ex instanceof kotlin.Exception
)
Copy code
TypeError: Cannot read properties of undefined (reading 'Exception')
Have you already try something similar? Why is it not provided by KotlinJs?
t
You can use browser
Error
instead
g
I don't understand how.
kotlin.Error
is also defined in stdlib-js but not exposed in the generated typescript, so I expect the same issue.
Just to be clear about my need, I want to expose a class (developed in Kotlin) that returns Exception, and on the typescript side be able to write
Copy code
if (exception instanceof kotlin.IllegalArgumentException) doStuff()
From what I tested I can use
Copy code
if (exception.name == "IllegalArgumentException") doStuff()
But then I cannot check for inheritance 😞
If I change my signature to
Error
, typescript exposes
Copy code
buildIndexOutOfBoundsException(msg: string): kotlin.Error;
with
kotlin.Error
instead of
Error
. 😞
t
Looks like
Throwable
in Kotlin ==
Error
in JS
👀 1