Bug Report
🔎 Search Terms
importsNotUsedAsValues, enum, import type
🕗 Version & Regression Information
- This is the behaviour in every version (≥3.8) I tried, and I reviewed the FAQ for entries about
importsNotUsedAsValues.
⏯ Playground Link
Bug workbench link with relevant code
Or paste the code snippets below into the TypeScript Twoslash tool—note that they do not work in the regular playground because (contrary to its own documentation) it apparently does not actually support twoslash markup.
💻 Code
A module which uses an enum imported from another module in the definition of a new enum is treated as if it does not use the enum object, producing the error "This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'":
// @importsNotUsedAsValues: error
// @errors: 1371
// @filename: foo.ts
export enum Foo {
a = 1,
b,
c,
}
// @filename: bar.ts
import {Foo} from './foo.js';
export enum Bar {
a = Foo.a,
c = Foo.c,
e = 5,
}
On the other hand, if the enum is obtained via import type then the error "'Foo' cannot be used as a value because it was imported using 'import type'" is generated.
// @importsNotUsedAsValues: error
// @errors: 1361
// @filename: foo.ts
export enum Foo {
a = 1,
b,
c,
}
// @filename: bar.ts
import type {Foo} from './foo.js';
export enum Bar {
a = Foo.a,
c = Foo.c,
e = 5,
}
🙁 Actual behaviour
Neither import nor import type can be used to import an enum used only to define another enum when importsNotUsedAsValues is set to 'error'.
🙂 Expected behaviour
The first example should not generate any errors, because it explicitly references the imported enum as an object.
Failing that, the second example should not generate any errors. This would be confusing and counterintuitive because it depends on the (to me surprising) fact that the generated code for Bar does not actually reference Foo but instead directly inlines values from Foo despite Foo not being a const enum. Nevertheless, it would be an acceptable since the error message from the first example specifically directs one to use import type.
Bug Report
🔎 Search Terms
importsNotUsedAsValues, enum, import type
🕗 Version & Regression Information
importsNotUsedAsValues.⏯ Playground Link
Bug workbench link with relevant code
Or paste the code snippets below into the TypeScript Twoslash tool—note that they do not work in the regular playground because (contrary to its own documentation) it apparently does not actually support twoslash markup.
💻 Code
A module which uses an
enumimported from another module in the definition of a new enum is treated as if it does not use the enum object, producing the error "This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'":On the other hand, if the
enumis obtained viaimport typethen the error "'Foo' cannot be used as a value because it was imported using 'import type'" is generated.🙁 Actual behaviour
Neither
importnorimport typecan be used to import anenumused only to define anotherenumwhenimportsNotUsedAsValuesis set to'error'.🙂 Expected behaviour
The first example should not generate any errors, because it explicitly references the imported
enumas an object.Failing that, the second example should not generate any errors. This would be confusing and counterintuitive because it depends on the (to me surprising) fact that the generated code for
Bardoes not actually referenceFoobut instead directly inlines values fromFoodespiteFoonot being aconst enum. Nevertheless, it would be an acceptable since the error message from the first example specifically directs one to useimport type.