Skip to content

Type Inference for Mapped Union Types Fails #19897

Closed
@fongandrew

Description

@fongandrew

TypeScript Version: 2.6.1 / 2.7.0-dev20171109

Code

type MapToNum<T> = {
  [K in keyof T]?: number|Array<number>;
};

function test<T>(m: MapToNum<T>, k: keyof MapToNum<T>) {
  let y = m[k];
  let z: number[] = [];
  if (y instanceof Array) {
    // Works, but y has type any[], should be number[]
    z = z.concat(y);
  } else if (y) {
    // Error, y has type number|number[], should just be number
    z.push(y);
  }
}

Expected behavior: instanceof Array should narrow number|number[]|undefined to number[] and the else if (y) should narrow further to number.

Actual behavior: Narrowed to any[] and number|number[] respectively.

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