-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
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.