Skip to content

importsNotUsedAsValues prevents importing an enum type to use only for declaring another enumΒ #49974

@cpcallen

Description

@cpcallen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions