question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update Pick to use new key remapping in mapped types

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tjjfvicommented, May 28, 2021

@bfricka The problem in that scenario boils down to Omit’s definition:

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

Because A has an index signature, that becomes Pick<A, string | number>. Homomorphic mapped types, on the other hand, have special casing for index signatures that maps them correctly.

#41966 looks like it might be relevant to your scenario.

0reactions
bfrickacommented, Mar 24, 2021

For one thing I noticed this fixes issues with Omit or Pick + intersection types in some cases. I’m not smart enough to know what this is called, but when a Pick or Omit type is fed into certain generics, it gets reduced or simplified in ways I don’t fully understand. I’ve seen it in various cases, but here’s a playground example.

You can see that passing an omit type into a remapped type causes an issue, but this new way of doing it fixes the issue. I wish I understood why, but perhaps someone could shed some light on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Mapped Types - TypeScript
A mapped type is a generic type which uses a union of PropertyKey s (frequently created via a keyof ) to iterate through...
Read more >
Mastering TypeScript mapped types - LogRocket Blog
In this post, we'll cover mapped types in TypeScript, a real-world example of them, and utility types including Partial, Readonly, and Pick.
Read more >
Use TypeScript Mapped Types Like a Pro
At this point, we can define a new UserPartial type that represents the type of user object to update, in which all keys...
Read more >
Mapped Types - Learn TypeScript w/ Mike North
Use with indexed access types. Pick. Mapping modifiers; Template ... What if we didn't want just any key to be used to store...
Read more >
Mapped types | NestJS - A progressive Node.js framework
As you build out features like CRUD (Create/Read/Update/Delete) it's often ... The PickType() function constructs a new type (class) by picking a set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found