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 turns optional parameters into required ones when union types are used #20722

Closed
kujon opened this issue Dec 15, 2017 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript
Milestone

Comments

@kujon
Copy link
Contributor

kujon commented Dec 15, 2017

TypeScript Version: 2.6.2

Code

type A = {
    foo?: number;
    bar: string;
};
type B = {
    foo?: number;
    bar: string;
    qux: boolean;
};
type JustOptionalFoo = Pick<A | B, 'foo'>;

// Type '{}' is not assignable to type 'Pick<A | B, "foo">'.
//   Property 'foo' is missing in type '{}'.
const a: JustOptionalFoo = {};

Expected behavior:
The above code compiles, since foo is optional in both type A and type B.

Actual behavior:
foo is turned into a required parameter.

@kujon kujon changed the title Pick turns optional parameters into required ones when union types are used: Pick turns optional parameters into required ones when union types are used Dec 15, 2017
@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 10, 2018
@mhegazy mhegazy added this to the Future milestone Jan 10, 2018
@OliverJAsh
Copy link
Contributor

I just ran into this. Here is another example.

type Foo =
  | {
      optionalProp?: string;
    }
  | {
      optionalProp?: string;
    };

// `optionalProp` is no longer optional
type Foo2 = Pick<Foo, keyof Foo>;

@laughinghan
Copy link

Related to #14295?

@jcalz
Copy link
Contributor

jcalz commented May 24, 2019

Ouch, yeah this just got me.

@Shinigami92
Copy link

This issue if fixed, can be closed
Tested with 3.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants