Home > other >  Typescript: Exporting Enum classes to JS produces compilation error
Typescript: Exporting Enum classes to JS produces compilation error

Time:11-08

I trying to export Kotlin Enum classes to JS

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

But in Angular Project, after importing as NPM module, the respective TS block in module_name.d.ts throws error during compilation when trying to run.

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;
}

tsconfig.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"
    ]
  }
}

Error from ng serve:

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 }

What are these errors ? Since I am new to JS and TS, I can't understand this. The editor not showing any errors.

If I remove the get keywords, then it's compiling successfully.

I can't understand what's wrong here. Please help me

CodePudding user response:

According to the official documentation for 1.7, it is prohibited to export enum classes.

This link provides some useful tips for exposing enums to TypeScript.

  • Related