Repository navigation
[Feature Request] Preserve comments when using Extract<keyof T, string> #31992
Copy link
Copy link
Open
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Search Terms
mapped type, preserve comment, keyof, Extract
Suggestion
type Mapped<T> = {
[k in keyof T]: ["some transformation", T[k]]
};
interface IFoo {
/** The string */
x: string;
/** The number */
y: number;
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x
//////////////////////////
type Mapped2<T> = {
[k in Extract<keyof T, string>]: ["some transformation", T[k]]
};
declare const mapped2IFoo: Mapped2<IFoo>;
//Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.xI'd like it if the fields of the mapped type could somehow preserve the comments of the fields of T, even after using Extract<keyof T, string>.
Use Cases
In my projects, there are many cases where I only want to deal with string keys and not symbol|number keys. So, I use Extract<keyof T, string> a lot. However, this does not preserve comments and makes me sad =(
Examples
//--noImplicitAny
type Mapped<T extends { [k: string]: any }> = {
[k in keyof T]: ["some transformation", T[k]]
};
const someSymbol: unique symbol = Symbol();
interface IFoo {
/** The string */
x: string;
/** The number */
y: number;
1: "i am a number";
[someSymbol] : "i am a symbol"
}
declare const mappedIFoo: Mapped<IFoo>;
//Tooltip shows "The string" as the comment
mappedIFoo.x;
//Is allowed, because we use `keyof T`
mappedIFoo[1];
//Is allowed, because we use `keyof T`
mappedIFoo[someSymbol];
//////////////////////////
type Mapped2<T extends { [k : string] : any }> = {
[k in Extract<keyof T, string>]: ["some transformation", T[k]]
};
declare const mapped2IFoo: Mapped2<IFoo>;
//Expected: Tooltip shows "The string" as the comment
//Actual: Tooltip DOES NOT show "The string" as the comment
mapped2IFoo.x;
//Expected: is not allowed
//Actual : Is not allowed; OK!
mapped2IFoo[1];
//Expected: is not allowed
//Actual : Is not allowed; OK!
mapped2IFoo[someSymbol];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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
AnyhowStep commentedon Jun 20, 2019
Workaround,
Playground
I don't like this workaround because the type ends up looking like this,
AnyhowStep commentedon Jun 20, 2019
Better workaround?
Playground
I don't like this workaround because the type ends up looking like this,
AnyhowStep commentedon Jun 20, 2019
I also don't like the workarounds because the constraint is
K extends keyof Tand notK extends Extract<keyof T, string>AnyhowStep commentedon Jun 20, 2019
This is my favourite workaround so far,
Playground
The type looks like this,
However, to get it to "look nice" and "work", I needed 3 types instead of 1, and weird indirection.
And I still don't like that the constraint is
K extends keyof T(even though leaving__Mapped5Impl<>unexported mitigates the risk ofKbeing changed to something else).AnyhowStep commentedon Jun 20, 2019
I'm kinda' obsessed with the constraint being
Extract<keyof T, string>because of something like this,Playground