Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wwoods opened this issue Sep 28, 2019 · 2 comments
Closed

Comments

@wwoods
Copy link

wwoods commented Sep 28, 2019

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?

@jack-williams
Copy link
Collaborator

Duplicate of #28339.

@wwoods
Copy link
Author

wwoods commented Sep 28, 2019

The documentation for pick need to make that really, really clear. That is an extremely odd implementation detail. But, ok, closing this issue. Thanks for the link.

@wwoods wwoods closed this as completed Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants