Typescript: Cannot export const enum in Export Declaration

Created on 23 Aug 2019  路  6Comments  路  Source: microsoft/TypeScript

TypeScript Version: 3.6.1-rc

Search Terms: const enum export declaration TS2475

Code

// test.ts
const enum Test {}
export { Test };

Expected behavior:
The above code compiles without errors.

Actual behavior:

test.ts:2:10 - error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.

Playground Link:
http://www.typescriptlang.org/play/#code/MYewdgzgLgBApmArgWxgFTtGBvAvgKDgA8AHEAJ1m3U1lwG4g

Related Issues:
N/A

Needs Investigation

Most helpful comment

I've noticed 3.7 beta is already announced yet this issue is still open. Should we consider this an undocumented Breaking change of 3.6 and live with it? or are there plans to get it fixed?

All 6 comments

Same code is working in 3.5.3. The issue is also present in typescript@next (3.7.0-dev.20190823).

Seems like it was introduced in #28498.

I looked into this some and the issue is with the check for preserveConstEnums in checker:checkConstEnumAccess. I removed the check locally and verified that it fixes the error. The only baselines that changed were for removing the errors. I am happy to send a PR for the change.

This issues is serious problem for me the following point. Do you plan to fix it?

  • Problem: Cannot re-export "const enum" which would like to hide the namespace but reuse one from .ts file.

@third/namespace.d.ts

export namespace HiddenThirdModule {

    export const enum ReuseConst {
        AAA = 'aaa',
        BBB = 'bbb',
    }
}

exports.ts

import { HiddenThirdModule } from '@third';

import ReuseConst = HiddenThirdModule.ReuseConst;
export { ReuseConst };  // error: ts2475
  • The follow cases are no problem.
  1. direct re-export

@third/enum.d.ts

export const enum ReuseConst {
    AAA = 'aaa',
    BBB = 'bbb',
}

exports.ts

import { ReuseConst } from '@third';
// export { ReuseConst }; // error: ts2475
export { ReuseConst } from '@third';
  1. from d.ts re-export
    exports .d .ts
import { ReuseConst } from '@third';
export { ReuseConst }; // no-error

I've noticed 3.7 beta is already announced yet this issue is still open. Should we consider this an undocumented Breaking change of 3.6 and live with it? or are there plans to get it fixed?

Hi, stranger!

Assume are you looking for soluton
TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query. ? 馃ぃ

1) Enable --preserveConstEnums in tsconfig.
2) If you're export constant from other file, doesn't use named import, like this:

// constants.ts
export const enum MyEnum {
  A, B, C,
}
// index.ts
import { MyEnum } from './constants.ts'; // <-- Named import
console.log(MyEnum); // Error



md5-a7e782bd4c0e793e7117c9e8737e1876




These steps allow me use enums as enumerable JavaScript object in TypeScript 3.9.5.
I spend lot of time to find solution and I hope it help you with your issue 鉂わ笍 

UDP:
Solution with named imports:



md5-be764a89d410f80fb37fe208bfa8ec68



```javascript
// index.ts
import { MyEnum } from './constants.ts';
const myEnum = MyEnum as const;
console.log(myEnum ); // Ok
Was this page helpful?
0 / 5 - 0 ratings