Skip to content

Parameter not matching union type not recognized when passed by reference #32270

Closed
@mr-flannery

Description

@mr-flannery

TypeScript Version: 3.3.3333, 3.4.2, 3.4.5, 3.5.2, 3.6.0-dev.20190704

Search Terms: union, union + object, union + object + bug

Code

interface A {
  a: string;
}

interface B {
  b: string;
}

const fn = (param: A | B) => param;

const obj = { a: "sdf", c: "sdf" };

fn({ a: "sd" });
fn({ b: "sd" });
fn({ a: "sd", b: "sfg" });
fn({ a: "sdf", c: "sdf" }); // this makes tsc fail
fn(obj); // this does not, but it should

Expected behavior:

tsc reports two errors, one for the last two lines respectively, as the parameters passed are essentially the same.

Actual behavior:

tsc only reports the second to last line as error, not the last one (or differently: if I comment the second to last line out, tsc does not fail, though it should)

Playground Link: https://www.typescriptlang.org/play/index.html#code/JYOwLgpgTgZghgYwgAgILIN4ChnLgLmQGcwpQBzAbiwF8stRJZEUAhTHZAI0JLJCq16CAPYgSyGCGQBeZAAoADnChwAtoXQAfZKwCUsgHzJlqtdSyjxYZCK4ArWZjyEAREQAmMVwBpkCN09vZBoLKXkMF2R3D1cQvWpwyJ5ozziaBKwkqJjfbkCYcnTM7IJUrzyA8uCMxJB5O3tMoA

Thank you very much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions