Skip to content

Distribute property type union members to create union of object types #36969

@ethanresnick

Description

@ethanresnick

TypeScript Version: v3.9.0-dev.20200222

Search Terms: union type object property

Code

type Test =
  | { b: [false, "a"] }
  | { b: [true, "b"] }
  
declare const b2: boolean;
const a: Test = { b: b2 ? [b2, "b"] : [b2, "a"] }

Expected behavior:
No error. I'm not exactly sure if this is a bug or feature request but, essentially, it'd be nice if TS could recognize that: { b: [true, "b"] | [false, "a"]; } is identical to { b: [true, "b"] } | { b: [false, "a"]; }

Actual behavior:

Type '{ b: [true, "b"] | [false, "a"]; }' is not assignable to type 'Test'.
  Type '{ b: [true, "b"] | [false, "a"]; }' is not assignable to type '{ b: [true, "b"]; }'.
    Types of property 'b' are incompatible.
      Type '[true, "b"] | [false, "a"]' is not assignable to type '[true, "b"]'.
        Type '[false, "a"]' is not assignable to type '[true, "b"]'.
          Type 'false' is not assignable to type 'true'.

Playground Link: Playground Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolution

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions