Skip to content

Pick turns optional parameters into required ones when union types are used #20722

@kujon

Description

@kujon

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions