-
Couldn't load subscription status.
- Fork 13.1k
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.
TemaSM
Metadata
Metadata
Assignees
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript