-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
infer Self #53690
Comments
I have a hard time figuring out from the examples what you're trying to do. Are you looking for the Like, for this example: type DoesTheThing<T> = T extends Something ? T : never
const something = aLongVariableName as DoesTheThing<typeof aLongVariableName>
type DoesTheThing = infer Self extends Something ? Self : never
const something: DoesTheThing = aLongVariableName You could have written this? const something: Something = aLongVariableName |
Sorry I made a mistake here. I meant: T extends Something<T> ? T : never I will correct it. |
So that's const something = aLongVariableName satisfies DoesTheThing<unknown>; or interface Something<T> {
x: T
}
const aLongVariableName = { x: 3 } satisfies Something<unknown>; |
The feature request sounds a lot like what has been PRed here |
@Andarist That does indeed seem very similar. I should've looked more carefully. With that being said I did come up with some different variations of the same general idea which might be useful. I've posted a comment on that PR. |
Suggestion
π Search Terms
Automatic generic parameter
Generic type self reference
Generic Self keyword
Infer Self
Exclude from any
β Viability Checklist
My suggestion meets these guidelines:
[x] (I think) This wouldn't be a breaking change in existing TypeScript/JavaScript code
[x] (if done right) This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
[x] This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Instead of writing
and then
it would be nice to have a way to infer the generic type in these instances.
Like this:
which could then be used like this:
With the risk of being off-topic another thing that would be nice, albeit not as nice as the above, is the ability to Exclude from any.
I have three variations of my main suggestion, infer Self, that carry varying risks:
infer Self
has to be in the beginning of the type definition.π Motivating Example
π» Use Cases
Google
jsonable typescript
to get lots of examples, the top result being this. There's also been another typescript suggestion to add JSON as a default type.In the article, the author suggests doing this:
This works but it's a bit verbose for what I wanted. I also wanted to allow undefined and null, and in that case basically anything can be JSON serialized except for functions and symbols if I'm not mistaken.
So I went on to google on how to make a NotFunction type. This was the top result, and they suggest doing this:
Which brings me to what I've already said.
The text was updated successfully, but these errors were encountered: