Skip to content

Enum literals assignments are inferred differently than what is emitted in .d.ts. #25990

Closed
@rkirov

Description

@rkirov

TypeScript Version: 3.1.0-dev.20180726

Search Terms:

enum literals declarations

Code

enum E {
  A = 'a',
  B = 'b'
}

class C {
  readonly type = E.A;
}

let x: E.A = new C().type; // no error, type is inferred to E.A, not E.

Expected behavior:
I expect similarly the .d.ts emitted to have a narrowed declaration for 'type'

declare class C {
    readonly type: E.A;
}

Actual behavior:
Emitted .d.ts contains:

declare class C {
    readonly type: E;
}

Note this is critical for creating tagged unions for type narrowing, and this behavior makes type narrowing only work when one is not crossing a '.d.ts' boundary. Bazel compliations usually look like 2-3 .ts files and hundreds to thousands .d.ts files from downstream compilations.

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts files

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions