-
Notifications
You must be signed in to change notification settings - Fork 12.8k
const
Type Parameters fail when mapped
#54537
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
Looking at the definition, this seems like arguably the intended behavior. Without reading the commentary here, I might think that someone did all this work in |
I have a workaround for you. - declare function doSomething<const Self>(o: WithSchema<Self>): void;
+ declare function doSomething<const Self>(o: [Self] extends [unknown] ? WithSchema<Self> : Self): void; I'm not quite sure but it seems that if you have a naked |
@RyanCavanaugh Yeah, that does make sense. @whzx5byb Ooh, that's perfect! I've reworked it a bit to make it more readable and more reusable. Here's /**
* Hints TS to infer `Self` as the object being typed, but actually resolves to
* `T`. (Generally, `T` should be defined in terms of `Self` to be useful.)
*/
type InferringSelf<Self, T> = Self extends unknown ? T : Self;
declare function doSomething<const Self>(
o: InferringSelf<Self, WithSchema<Self>>
): void; This also takes over all of the I'm happy considering this a real solution and not just a workaround. @RyanCavanaugh, I didn't see anything in your comment that sounded like you saw what you'd consider a bug here, so I'll close. If you did see something worth addressing, obviously feel free to re-open. Thanks, both! ❤️🎉 |
…ombinations; increate max items to 100, was erroring because I was using an old version of ddb local (https://aws.amazon.com/about-aws/whats-new/2022/09/amazon-dynamodb-supports-100-actions-per-transaction/); note that all of this only works because of the workaround described in this issue microsoft/TypeScript#54537
Bug Report
🔎 Search Terms
const
Type Parameters, mapped types, self types🕗 Version & Regression Information
const
Type Parameters (but there's nothing there about them yet)⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
doSomething
'sSelf
parameter was not inferred asconst
.🙂 Expected behavior
doSomething
'sSelf
parameter should be inferred asconst
, since it's marked asconst
and inferred from a literal at the call site.More context
I'm actually working on something more complex than this: typing JSON-LD documents. The core of the issue, though, is that the type of some keys of the object depends on another key (here,
schema
). I'm doing this using a type parameter which infers as the type of the given object. This uses the same technique described in #52088 (which proposes a more native version using a new keyword).Currently, the only way to make these types useful is to ask the user of these types to use
as const
everywhere. Of course, this is less than ideal, which is exactly whyconst
Type Parameters were introduced. But it appears the more complex usage I have breaks that feature, reverting to inferring a widened type.The text was updated successfully, but these errors were encountered: