Type metadata / shadow types #46142
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
type metadata info secondary
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Create a type that in a union, has no effect on the result of the union (same as never), but can be retrieved using a special type (i.e.
GetMetadata<T>
).π Motivating Example
This can be used to document errors which a function may throw.
Currently, Typescript allows to do something like the below, but there's an issue in that the Throws<> type pollutes the function return type which has to be manually eliminated by type casting - not very practical.
This is a full example of what I'm aiming at, from my comment in the issue #13219 (comment)
Playground
With the new special
Metadata<T>
utility type it would possible to store such information and access it with a special typeGetMetadata<T>
while the type likenumber | string | Metadata<unknown>
would be justnumber | string
.π» Use Cases
What do you want to use this for?
Documenting what types of errors a function may throw.
What shortcomings exist with current approaches?
It's possible to use a helper type to eliminate the metadata from the union, but it's too verbose to be practical. It could also be stored in a separate type, but with classes, it would be away from the method body and also not practical enough to consider.
What workarounds are you using in the meantime?
I'm not documenting the types of errors a function may throw at all or I use tsdoc throws (but it does not allow to store type info).
The text was updated successfully, but these errors were encountered: