Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rkirov opened this issue Jul 26, 2018 · 3 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files

Comments

@rkirov
Copy link
Contributor

rkirov commented Jul 26, 2018

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:

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Jul 26, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 26, 2018

Duplicate of #15881

@mhegazy mhegazy marked this as a duplicate of #15881 Jul 26, 2018
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Jul 26, 2018
@weswigham weswigham removed the Duplicate An existing issue was already created label Aug 8, 2018
@weswigham
Copy link
Member

weswigham commented Aug 8, 2018

Not quite a duplicate - enum references aren't currently allowed as constant initializers in declaration files.

@weswigham weswigham added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Aug 8, 2018
@weswigham weswigham self-assigned this Aug 8, 2018
@weswigham weswigham added this to the TypeScript 3.1 milestone Aug 8, 2018
@rkirov
Copy link
Contributor Author

rkirov commented Aug 10, 2018

Thanks for picking the up. We are seeing a lot of code using this pattern because it is what ngrx (redux for angular) is recommending - https://github.com/ngrx/platform/blob/master/docs/store/actions.md#typed-actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files
Projects
None yet
Development

No branches or pull requests

4 participants