-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[Feature Request] Preserve comments when using Extract<keyof T, string> #31992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Workaround, //--noImplicitAny
type Mapped3<
T extends { [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`
*/
K extends keyof T = Extract<keyof T, string>
> = {
[k in K]: ["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 mapped3IFoo: 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]; I don't like this workaround because the type ends up looking like this,
|
Better workaround? //--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<
T extends { [k: string]: any },
/*
We use a little hack to get `mapped4IFoo.x` to show its comment.
*/
K extends keyof T = Extract<keyof T, string>
> = {
[k in K]: ["some transformation", T[k]]
};
type Mapped4<T extends { [k: string]: any }> = __Mapped4Impl<T>;
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 mapped4IFoo: 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]; I don't like this workaround because the type ends up looking like this,
|
I also don't like the workarounds because the constraint is |
This is my favourite workaround so far, //--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<
T extends { [k: string]: any },
/*
We use a little hack to get `mapped5IFoo.x` to show its comment.
*/
K extends keyof T=Extract<keyof T, string>
> = _<{
[k in K]: ["some transformation", T[k]]
}>;
type Mapped5<T extends { [k: string]: any }> = __Mapped5Impl<T>;
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"
}
/**
Tooltip shows,
const mapped5IFoo: { x: ["some transformation", string]; y: ["some transformation", number]; }
*/
declare const mapped5IFoo: 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]; The type looks like this, const mapped5IFoo: { x: ["some transformation", string]; y: ["some transformation", number]; } 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 |
I'm kinda' obsessed with the constraint being type _<T> = T;
type TakesNumber<N extends number> = (
N extends number ?
["some transformation", N] :
["Expected number, received", N]
)
type Mapped6<
T extends { [k: string]: number }
> = _<{
//TS thinks `T[k] extends number` but it may not be the case!
[k in keyof T]: TakesNumber<T[k]>
}>;
const someSymbol: 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"];
}
*/
declare const mapped6IFoo: Mapped6<{
x: number,
y: number,
[someSymbol] : "string value of symbol key",
}>; |
Search Terms
mapped type, preserve comment, keyof, Extract
Suggestion
Playground
I'd like it if the fields of the mapped type could somehow preserve the comments of the fields of
T
, even after usingExtract<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 useExtract<keyof T, string>
a lot. However, this does not preserve comments and makes me sad =(Examples
Playground
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: