Skip to content

In assignment Type Union is resolved as Intersection  #38159

@askirmas

Description

@askirmas

TypeScript Version: Nightly

Search Terms:
Type Union, Type check during assignment
Expected behavior:
Some error i.e.

19:7 - error TS2339: Property 'c' does not exist on type 'tAB'.

19 c: false,

Actual behavior:
exits with status 0

Related Issues:

Code

type tAB = {
  a: string
  b: number
}
type tAC = {
  a: string
  c: boolean
}

type tUnion = tAB | tAC

//@ts-ignore Type '{}' is not assignable to type 'tUnion'
const empty: tUnion = {}

//No ts-error but should be
const union: tUnion = {
  a: '',
  b: 0,
  c: false,
}
//@ts-ignore  Property 'b' does not exist on type 'tAC'
union.b
Output
"use strict";
//@ts-ignore Type '{}' is not assignable to type 'tUnion'
const empty = {};
//No ts-error but should be
const union = {
    a: '',
    b: 0,
    c: false,
};
//@ts-ignore  Property 'b' does not exist on type 'tAC'
union.b;
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions