kotlin Typescript:将枚举类导出到JS时产生编译错误

f0ofjuux  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(221)

我尝试将Kotlin枚举类导出到JS

@OptIn(ExperimentalJsExport::class)
@JsExport
enum class interEnum {
    SAMPLE
}

但在Angular项目中,导入为NPM模块后,module_name.d.ts中的相应TS块在编译期间尝试运行时抛出错误。

abstract class interEnum { // exported from Kotlin/JS 
    private constructor();
    static get SAMPLE(): com.example.demoapp.interEnum & {
        get name(): "SAMPLE";
        get ordinal(): 0;
    };
    static values(): Array<com.example.demoapp.interEnum>;
    static valueOf(value: string): com.example.demoapp.interEnum;
    get name(): "SAMPLE";
    get ordinal(): 0;
}

服务器配置.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

ng服务出错:

ERROR in demo_app/demo_app.d.ts:286:13 - error TS1131: Property or signature expected.

286             get name(): "SAMPLE";
                ~~~
demo_app/demo_app.d.ts:289:9 - error TS1128: Declaration or statement expected.

289         static values(): Array<com.example.demoapp.interEnum>;
            ~~~~~~
demo_app/demo_app.d.ts:289:24 - error TS1005: ';' expected.

289         static values(): Array<com.example.demoapp.interEnum>;
                           ~
demo_app/demo_app.d.ts:289:62 - error TS1005: '(' expected.

289         static values(): Array<com.example.demoapp.interEnum>;
                                                                 ~
demo_app/demo_app.d.ts:290:9 - error TS1128: Declaration or statement expected.

290         static valueOf(value: string): com.example.demoapp.interEnum;
            ~~~~~~
demo_app/demo_app.d.ts:290:29 - error TS1005: ',' expected.

290         static valueOf(value: string): com.example.demoapp.interEnum;
                                ~
demo_app/demo_app.d.ts:290:38 - error TS1005: ';' expected.

290         static valueOf(value: string): com.example.demoapp.interEnum;
                                         ~
demo_app/demo_app.d.ts:291:13 - error TS1005: ';' expected.

291         get name(): "SAMPLE";
                ~~~~
demo_app/demo_app.d.ts:291:19 - error TS1005: ';' expected.

291         get name(): "SAMPLE";
                      ~
demo_app/demo_app.d.ts:292:13 - error TS1005: ';' expected.

292         get ordinal(): 0;
                ~~~~~~~
demo_app/demo_app.d.ts:292:22 - error TS1005: ';' expected.

292         get ordinal(): 0;
                         ~
demo_app/demo_app.d.ts:294:1 - error TS1128: Declaration or statement expected.

294 }

这些错误是什么?因为我是JS和TS的新手,我不明白这些。编辑器没有显示任何错误。
如果我删除了get关键字,那么它就成功编译了。
我不明白这是怎么回事。请帮帮我

vtwuwzda

vtwuwzda1#

根据官方的documentation for 1.7,禁止导出枚举类。
link提供了一些向TypeScript公开枚举的有用提示。

相关问题