Skip to content

'const enum' conflict with 'importsNotUsedAsValue'' #36003

Description

TypeScript Version: 3.8.0-dev.20200104

Search Terms:

Code

// @importsNotUsedAsValue: true

// @filename: a.ts
export const enum E {
  One,
  Two,
}

// @filename: b.ts
import type {E} from './a';

export let v = E.One;

Expected behavior:

No error.

Actual behavior:

Type-only import causes Enum 'E' cannot be used as a value because only its type has been imported.ts(1362)
Since importing const enums at runtime is not necessary due to inlining, they should be allowed to be used with type-only import.

If I use a regular import, I get This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValue' is set to 'error'.ts(1371)

Related Issues: Introduced by Andrew Branch (@andrewbranch) in #35200

Activity

  1. ajafff commented on Jan 4, 2020

    @ajafff
    ContributorAuthor

    Also note that the type-only import of the const enum is reported as unused.

  2. andrewbranch commented on Jan 6, 2020

    @andrewbranch
    Member

    I see the motivation, but import type means more than just “don’t emit this import declaration.” It filters the symbol meanings down to just type and type-containing namespace meanings. We’re currently discussing a similar scenario:

    import type { BaseClass } from './a'
    declare class C extends BaseClass {} // Should this be treated as having a value?

    and the instincts on the team are mixed. But using a type-only import in the right-hand side of an assignment looks really unexpected to me.

  3. andrewbranch commented on Jan 6, 2020

    @andrewbranch
    Member

    If I use a regular import, I get This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValue' is set to 'error'.ts(1371)

    This part sounds like a bug

  4. falsandtru commented on Jan 6, 2020

    @falsandtru
    Contributor

    I believe the error Enum 'E' cannot be used as a value because only its type has been imported.ts(1362) is correct. You must not use a type as a value. And I believe base classes of ambient classes should be treated as having no value because ambient declarations just declare types of existing values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions