Skip to content

Pick doesn't preserve optional from unions #28483

Closed
@RomanHotsiy

Description

@RomanHotsiy

TypeScript Version: 3.2.0-dev.20181110

Search Terms: Pick preserve optional union

Code

type A = {
  optional?: string;
  other: string;
}

type B = {
  optional?: string;
  other: string;
}

type SimplePick = Pick<A, 'optional'>
/*
{
    optional?: string | undefined;
}
*/

type PickUnion = Pick<A | B, 'optional'>
/*
{
    optional: string | undefined; // <--- note it is not optional
}
*/

type A = {
  optional?: string;
  other: string;
}

type B = {
  optional?: string;
  other: string;
}

let good: SimplePick = {}; // works just fine
let fails: PickUnion = {}; // Property 'optional' is missing in type '{}'.

Expected behavior:
Pick should preserve optional property when used on unions

Actual behavior:
It doesn't

Playground Link: link

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Mapped TypesThe issue relates to mapped typesNeeds InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions