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

Union type checking during assignment fails for boolean and passes for other primitives #35861

Open
evxn opened this issue Dec 26, 2019 · 0 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@evxn
Copy link

evxn commented Dec 26, 2019

TypeScript Version: 3.7.2

Search Terms:
number string union

Expected behavior:

Compiler should be giving an error on assigning demo1, like it does with demo2.

Actual behavior:

Doesn't seem to properly discriminate between the string and other types that are conceptually unbound: number/RegExp etc.

Related Issues:
#20863
#30506
#9254

Code

type t1 = {
    a: number;
    b: string;
} | {
    a: string;
};

// no error. Invalid assignment is possible
const demo1: t1 = {
    a: '123',
    b: 'asd',
};

type t2 = {
    a: boolean;
    b: string;
} | {
    a: string;
};

// error Error: TS2322: Type '{ a: string; b: string; }' is not assignable to type 't2'.  Object literal may only specify known properties, and 'b' does not exist in type '{ a: string; }'.
const demo2: t2 = {
    a: '123',
    b: 'asd',
};
Output
"use strict";
// no error
const demo1 = {
    a: '123',
    b: 'asd',
};
// error
const demo2 = {
    a: '123',
    b: 'asd',
};
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@evxn evxn changed the title Union type narrowing assignment fails for boolean and works for other primitives Union type narrowing during assignment fails for boolean and works for other primitives Dec 26, 2019
@evxn evxn changed the title Union type narrowing during assignment fails for boolean and works for other primitives Union type checking during assignment fails for boolean and works for other primitives Dec 26, 2019
@evxn evxn changed the title Union type checking during assignment fails for boolean and works for other primitives Union type checking during assignment fails for boolean and passes for other primitives Dec 26, 2019
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 13, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants