You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request] Preserve comments when using Extract<keyof T, string> #31992
typeMapped<T>={[kinkeyofT]: ["some transformation",T[k]]};interfaceIFoo{/** The string */x: string;/** The number */y: number;}declareconstmappedIFoo: Mapped<IFoo>;//Tooltip shows "The string" as the commentmappedIFoo.x//////////////////////////typeMapped2<T>={[kinExtract<keyofT,string>]: ["some transformation",T[k]]};declareconstmapped2IFoo: Mapped2<IFoo>;//Tooltip DOES NOT show "The string" as the commentmapped2IFoo.x
I'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
//--noImplicitAnytypeMapped<Textends{[k: string]: any}>={[kinkeyofT]: ["some transformation",T[k]]};constsomeSymbol: unique symbol=Symbol();interfaceIFoo{/** The string */x: string;/** The number */y: number;1: "i am a number";[someSymbol] : "i am a symbol"}declareconstmappedIFoo: Mapped<IFoo>;//Tooltip shows "The string" as the commentmappedIFoo.x;//Is allowed, because we use `keyof T`mappedIFoo[1];//Is allowed, because we use `keyof T`mappedIFoo[someSymbol];//////////////////////////typeMapped2<Textends{[k : string] : any}>={[kinExtract<keyofT,string>]: ["some transformation",T[k]]};declareconstmapped2IFoo: Mapped2<IFoo>;//Expected: Tooltip shows "The string" as the comment//Actual: Tooltip DOES NOT show "The string" as the commentmapped2IFoo.x;//Expected: is not allowed//Actual : Is not allowed; OK!mapped2IFoo[1];//Expected: is not allowed//Actual : Is not allowed; OK!mapped2IFoo[someSymbol];
//--noImplicitAnytypeMapped3<Textends{[k: string]: any},/* We use a little hack to get `mapped3IFoo.x` to show its comment. I don't like this hack because someone could easily mess it up by modifying the value of `K` */KextendskeyofT=Extract<keyofT,string>>={[kinK]: ["some transformation",T[k]]};constsomeSymbol: unique symbol=Symbol();interfaceIFoo{/** The string */x: string;/** The number */y: number;1: "i am a number";[someSymbol] : "i am a symbol"}declareconstmapped3IFoo: Mapped3<IFoo>;//Expected: Tooltip shows "The string" as the comment//Actual: Tooltip shows "The string" as the comment; OK!mapped3IFoo.x;//Expected: is not allowed//Actual : Is not allowed; OK!mapped3IFoo[1];//Expected: is not allowed//Actual : Is not allowed; OK!mapped3IFoo[someSymbol];
//--noImplicitAny/** DO NOT USE THIS TYPE DIRECTLY! USE `Mapped4<>` INSTEAD! ----- We can probably export `Mapped4<>` and not export `__Mapped4Impl<>` to force users to only use `Mapped4<>`*/type__Mapped4Impl<Textends{[k: string]: any},/* We use a little hack to get `mapped4IFoo.x` to show its comment. */KextendskeyofT=Extract<keyofT,string>>={[kinK]: ["some transformation",T[k]]};typeMapped4<Textends{[k: string]: any}>=__Mapped4Impl<T>;constsomeSymbol: unique symbol=Symbol();interfaceIFoo{/** The string */x: string;/** The number */y: number;1: "i am a number";[someSymbol] : "i am a symbol"}declareconstmapped4IFoo: Mapped4<IFoo>;//Expected: Tooltip shows "The string" as the comment//Actual: Tooltip shows "The string" as the comment; OK!mapped4IFoo.x;//Expected: is not allowed//Actual : Is not allowed; OK!mapped4IFoo[1];//Expected: is not allowed//Actual : Is not allowed; OK!mapped4IFoo[someSymbol];
//--noImplicitAny/** We use this no-op `_` type as a hack to get the tooltip to give us a "better-looking" type*/type_<T>=T;/** DO NOT USE THIS TYPE DIRECTLY! USE `Mapped5<>` INSTEAD! ----- We can probably export `Mapped5<>` and not export `__Mapped5Impl<>` to force users to only use `Mapped5<>`*/type__Mapped5Impl<Textends{[k: string]: any},/* We use a little hack to get `mapped5IFoo.x` to show its comment. */KextendskeyofT=Extract<keyofT,string>>=_<{[kinK]: ["some transformation",T[k]]}>;typeMapped5<Textends{[k: string]: any}>=__Mapped5Impl<T>;constsomeSymbol: unique symbol=Symbol();interfaceIFoo{/** The string */x: string;/** The number */y: number;1: "i am a number";[someSymbol] : "i am a symbol"}/** Tooltip shows, const mapped5IFoo: { x: ["some transformation", string]; y: ["some transformation", number]; }*/declareconstmapped5IFoo: Mapped5<IFoo>;//Expected: Tooltip shows "The string" as the comment//Actual: Tooltip shows "The string" as the comment; OK!mapped5IFoo.x;//Expected: is not allowed//Actual : Is not allowed; OK!mapped5IFoo[1];//Expected: is not allowed//Actual : Is not allowed; OK!mapped5IFoo[someSymbol];
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 of K being changed to something else).
I'm kinda' obsessed with the constraint being Extract<keyof T, string> because of something like this,
type_<T>=T;typeTakesNumber<Nextendsnumber>=(Nextendsnumber ?
["some transformation",N] :
["Expected number, received",N])typeMapped6<Textends{[k: string]: number}>=_<{//TS thinks `T[k] extends number` but it may not be the case![kinkeyofT]: TakesNumber<T[k]>}>;constsomeSymbol: unique symbol=Symbol();/** const mapped6IFoo: { x: ["some transformation", number]; y: ["some transformation", number]; //Whoops! Using `keyof T` was not such a good idea! [someSymbol]: ["Expected number, received", "string value of symbol key"]; }*/declareconstmapped6IFoo: Mapped6<{x: number,y: number,[someSymbol] : "string value of symbol key",}>;
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