Skip to content

[js_interop] Can @JSExport annotation be applied to not a class? #61064

@sgrekhov

Description

@sgrekhov

From JSExport documentation it is not clear if enums, extension types and extensions can be annoteted with JSExport annotation or not. The following example works for me.

import 'dart:js_interop';
import 'dart:js_interop_unsafe';

@JS()
external void eval(String code);

@JSExport()
enum E {
  e0(0);

  final int id;
  const E(this.id);
}

@JSExport()
class C {
  int id = 42;
}

@JSExport()
extension type ET(C c) implements C {
}

@JSExport()
extension Ext on C {
  int extensionMethod() => 42;
}

void main() {
  var jsE = createJSInteropWrapper<E>(E.e0);
  var jsET = createJSInteropWrapper<C>(ET(C()));
  var jsC = createJSInteropWrapper<C>(C());

  globalContext["jsE"] = jsE;
  globalContext["jsET"] = jsET;
  globalContext["jsC"] = jsC;
  eval(r'''
    console.log(globalThis.jsE.id);     // 0
    console.log(globalThis.jsET.c);     // undefined
    console.log(globalThis.jsC.id);     // 42
    console.log(globalThis.jsC.extensionMethod);    // undefined
  ''');
}

@srujzs @osa1 Is the above intended and expected?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-questionA question about expected behavior or functionalityweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions