Skip to content
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

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

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

Comments

@wwoods
Copy link

@wwoods 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

This comment has been minimized.

Copy link
Collaborator

@jack-williams jack-williams commented Sep 28, 2019

Duplicate of #28339.

@wwoods

This comment has been minimized.

Copy link
Author

@wwoods 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 Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.