Skip to content

Update Pick to use new key remapping in mapped types #41383

Open
@tjjfvi

Description

@tjjfvi

Search Terms

pick omit key remapping

Suggestion

Update Pick in lib.d.ts to be:

type Pick<T, K extends keyof T> = {
    [P in keyof T as Extract<P, K>]: T[P];
};

It is currently defined as:

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

Use Cases

With this new definition, typescript can statically analyze the keys, which allows interfaces to extends these types.

Examples

interface Base {
    a: number,
    b: string,
    c: boolean,
}

// Errors: "An interface can only extend an object type or intersection of object types with statically known members"
interface PickBase0<K extends keyof Base> extends Pick<Base, K> {
    pickedKeys: K
}

type Pick1<T, K extends keyof T> = { [L in keyof T as Extract<L, K>]: T[L] }

// Works
interface PickBase1<K extends keyof Base> extends Pick1<Base, K> {
    pickedKeys: K
}

Playground Link

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions