Skip to content

Pick<TypeUnion, 'prop1' | 'prop2'> loses associations between 'prop1' and 'prop2' #33656

@wwoods

Description

@wwoods

TypeScript Version: 3.7.0-dev.20190928

Search Terms: Pick, union bug, pick union bug

Code

interface A {
  type: 'A';
  inner: {
    a: boolean,
  };
  detail: string;
}

interface B {
  type: 'b';
  inner: {
    b: boolean,
  };
  detail: string;
}

type Combined = A | B;

// Broken - typescript does not raise an error, despite the mismatch between type 'A' and inner 'b'
type DescriptionBroken = Pick<Combined, 'type' | 'inner'>;
let obj: DescriptionBroken = {type: 'A', inner: {b: true}};

// OK - typescript raises an error.
type DescriptionOk = Pick<A, 'type' | 'inner'> | Pick<B, 'type' | 'inner'>;
let objOk: DescriptionOk = {type: 'A', inner: {b: true}};

Expected behavior:
The let obj line under the // Broken comment should raise an error, but does not.

Actual behavior:

The let obj line under the // Broken comment does not raise an error, presumably due to a disassociation between the 'type' and 'inner' elements.

Playground Link: https://www.typescriptlang.org/play/?ts=Nightly#code/JYOwLgpgTgZghgYwgAgILIN4ChnLATwAcIAuZAclXIG4dlQRoztdc4yAjAey4BsI4IADR0AvrVwATCGDjBeZAM5gooAOa1RWLKEixEKAEKY6BYmXIcadBkxOtkHTj36CRucXWmz5Sles1tMxQAYS4AWw5QCElkAF40ZAAfZENaLAB6DNSoLgBrCBBkAFo8IghFBFVCMGRJLgrkEC5aqDlFFEFkaFyoITqKwmBIPAALFHDgRXC4MARRxxkAdwhCsuIKKmRBWNsoCissYOQAEQqq4BrgLhBDXIKihIAFYAQ8gB4wyOjJfvJg8jJCh7cgAPlo-FqXA4ACsyGdKtUwNdbvc1gkMMELFR+ntmE48FAAK4QUSeTLZADyAGkSutzkjkG0po0uj0uFAAHRHcqnBmXZE3Sl5eLIF5vd6oP4AoHkEGgoHij6GaXlQEpOUgRhQMEQmTIaEw4Xw-lXIUijFYzbkXFauwYAkqElk6hAA

Related Issues: I found a number relating to Pick, but none with such a clean example or this exact issue. Maybe #33568?

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