Update Pick to use new key remapping in mapped types
See original GitHub issueSearch 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
}
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:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bfricka The problem in that scenario boils down to
Omit
’s definition:Because
A
has an index signature, that becomesPick<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.
For one thing I noticed this fixes issues with
Omit
orPick
+ intersection types in some cases. I’m not smart enough to know what this is called, but when aPick
orOmit
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.