Skip to content

Native methods shouldn't be exported as if they are new types #111

@niloc132

Description

@niloc132

Sometimes it can be helpful to create a static private method and mark it with jsinterop to access some global value:

@JsType
public class MyExportedType {
    //...

    @JsType(isNative = true, namespace = JsPackage.GLOBAL)
    public interface PromiseSettled<T> {
        String getStatus();
        Object getReason();
        T getValue();
    }

    @JsMethod(namespace = "Promise", name = "allSettled")
    private static native <V> Promise<PromiseSettled<V>[]> allSettledInternal(IThenable<? extends V>[] promises);
}

However, this ends up creating a new Promise type inside my output, even though the method is native:

	export class Promise {
		static allSettled<V>(promises:IThenable<unknown>[]):Promise<PromiseSettled<V>[]>;
	}

Workaround:

Move the single private method to its own class, or any other type that isn't meant to be exported

@JsType
public class MyExportedType {
    //...

    @JsType(isNative = true, namespace = JsPackage.GLOBAL)
    public interface PromiseSettled<T> {
        String getStatus();

        Object getReason();

        T getValue();
    }

    private static class PromiseHelper {
        @JsMethod(namespace = "Promise", name = "allSettled")
        private static native <V> Promise<PromiseSettled<V>[]> allSettledInternal(IThenable<? extends V>[] promises);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions