dukat icon indicating copy to clipboard operation
dukat copied to clipboard

@nativeInvoke is deprecated

Open ts2kt opened this issue 6 years ago • 6 comments

export interface SomeInterface {
    (x: any, y: any): boolean;
}

produce:

external interface SomeInterface {
    @nativeInvoke // 'nativeInvoke' is deprecated. Use inline extension function with body using dynamic
    operator fun invoke(x: Any, y: Any): Boolean
}

ts2kt avatar Jun 05 '19 14:06 ts2kt

Well, @nativeInvoke is the only way to translate this correctly in my understanding so I'd rather investigate how to supress this warning for this particular declarations.

Schahen avatar Jun 05 '19 22:06 Schahen

Isn't it a problem of Kotlin JS than? I mean @nativeInvoke is deprecated, but it should be possible somehow to use this external interface without warnings from Kotlin JS Maybe make sense to create an issue on Kotlin issue tracker?

gildor avatar Jun 06 '19 02:06 gildor

It's not related with this issue, but I post it here (don't know how to name it)

export function getter<T>(_this: any, propertyValue: T | { (): T }): T;
external interface `T$1` {
    @nativeInvoke
    operator fun invoke(): T /* --- Error: Unresolved reference: T --- */
}
external fun <T> getter(_this: Any, propertyValue: T): T
external fun <T> getter(_this: Any, propertyValue: `T$1`): T

ts2kt avatar Jun 26 '19 17:06 ts2kt

@Schahen such callable Interface don't work for me (or maybe I don't understand how to use it in Kotlin). I have such d.ts:

export interface Template {
    (): string;
}

generated Kotlin declarations is:

external interface Template {
    @nativeInvoke
    operator fun invoke(): String
}

But such code don't work for me:

val prop: Template = object : Template {
    override fun invoke() = "text"
}

But such code works as expected:

typealias Template = () -> String

val prop: Template = () -> "text"

ts2kt avatar Jul 09 '19 15:07 ts2kt

This seems to work as expected:

external interface SomeInterface
inline operator fun SomeInterface.invoke(x: Any, y: Any): Boolean {
    return asDynamic()(x, y).unsafeCast<Boolean>()
}

Fiouz avatar Nov 14 '19 16:11 Fiouz

I'm running into this warning as well. Can anyone point me to some resource explaining why this is deprecated?

I have a similar TS declaration, but on top of that it uses optional parameters:

declare const SockJS: {
    new (url: string, _reserved?: any, options?: SockJS.Options): WebSocket;
    (url: string, _reserved?: any, options?: SockJS.Options): WebSocket;

    // ... other stuff
};

Dukat generates this:

external object SockJS {
    @nativeInvoke
    operator fun invoke(url: String, _reserved: Any = definedExternally, options: Options = definedExternally): WebSocket

    // ... other stuff
}

I don't believe it's even possible to use the inline extension approach with definedExternally default values. Does that mean I should declare all overloads manually myself?

joffrey-bion avatar Jan 03 '22 17:01 joffrey-bion