Skip to content

Nominal enum assignability after union #10776

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

Closed
ivogabe opened this issue Sep 8, 2016 · 4 comments
Closed

Nominal enum assignability after union #10776

ivogabe opened this issue Sep 8, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ivogabe
Copy link
Contributor

ivogabe commented Sep 8, 2016

TypeScript Version: 2.0 RC. This was working correctly in 2.0 beta.

Code

module X {
    export enum Foo {
        A, B
    }
}
module Y {
    export enum Foo {
        A, B
    }
}
const x: X.Foo = Y.Foo.A;
const y: X.Foo | undefined = Y.Foo.A;

Expected behavior:
Both assignments are valid.

Actual behavior:
The first assignment is valid, the second throws an error saying "Type 'Foo' is not assignable to type 'Foo | undefined'."

This block a project that depends on the TypeScript api, and on some package that also depends on TypeScript. So TS is installed on node_modules/typescript and node_modules/foo/node_modules/typescript. (Normally NPM would install TypeScript only once, but foo is a symlink.) This causes that objects from one installation of TypeScript cannot be passed to the other. The following code illustrates that:

import * as ts from "typescript";
import * as ts2 from "foo/node_modules/typescript";

const identifier: ts.Identifier = undefined as any;
const identifier2: ts2.Identifier = identifier;

const ok: ts.SyntaxKind = ts2.SyntaxKind.AbstractKeyword;
const error: ts.SyntaxKind | undefined = ts2.SyntaxKind.AbstractKeyword;

Error on the declaration of identifier2:

Type 'ts.Identifier' is not assignable to type 'ts.Identifier'.
  Types of property 'originalKeywordKind' are incompatible.
    Type 'SyntaxKind | undefined' is not assignable to type 'SyntaxKind | undefined'.
      Type 'SyntaxKind.Unknown' is not assignable to type 'SyntaxKind | undefined'.

And on error:

Type 'SyntaxKind' is not assignable to type 'SyntaxKind | undefined'.
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 8, 2016
@RyanCavanaugh
Copy link
Member

@mhegazy milestone?

@mhegazy mhegazy added this to the TypeScript 2.0.3 milestone Sep 8, 2016
@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

Note: requires "strictNullChecks": true to repro

@sandersn sandersn changed the title Nominal enum assignability after union Nominal enum assignability after union with strictNullChecks Sep 8, 2016
@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

repro without strictNullChecks: const y: X.Foo | boolean = Y.Foo.A;

@sandersn sandersn changed the title Nominal enum assignability after union with strictNullChecks Nominal enum assignability after union Sep 8, 2016
@sandersn
Copy link
Member

sandersn commented Sep 8, 2016

The fix is very similar to the one for #10738, so I'm going to fix both in #10784.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 12, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants