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
Extract interface metadata for runtime #58434
Comments
This is explicitly out of scope for TypeScript. Check the "Viability Checklist" again, specifically "emitting based on the type of expressions", "this isn't a runtime feature" and the Design Goals, goal 9 and non-goal 5. It's also been suggested and declined numerous times already.
That doesn't seem to be accurate. You should be able to do this: |
Ok I didn't know about the design goals page , thank you for pointing it out. This feature would not keep typescript "fully erasable". I close this issue.
You are correct I switched the words, I edited my first message to fix it. |
π Search Terms
β Viability Checklist
β Suggestion
I propose adding a feature to TypeScript that allows developers to extract interface descriptions at runtime for use in runtime type checking. This feature aims to reduce redundancy and streamline the process of ensuring runtime data matches predefined interfaces.
Something like:
To be clear, what I suggest is not having dynamic types; it is simply adding a kind of "sugar" to TypeScript to make interface information available at runtime, without having to duplicate the code.
π Motivating Example
When programming with TypeScript, a common task is to ensure that some unknown data obtained at runtime matches an interface. Currently, the go-to methods involve using TypeScript type guards or external libraries like Zod or yup. However, these solutions all share the same problem: you must duplicate your interface behavior in a system that allows performing the check at runtime.
As a developer, I do not like this redundancy. What I would prefer is a way to have a generic type guard based on the interface.
With this, feature, you could simply write this :
It is not totally generic, you still have to write one typeguard function by interface you want to validate, but it is way less painful than having to copy-paste all the interface into the guard.
π» Use Cases
What do you want to use this for?
Data Validation: Runtime type checking ensures that data received from external sources, like API responses or database queries, adheres to predefined interfaces without duplicating definitions.
Improved Developer Experience: Eliminate the need for duplicating interface definitions, leading to cleaner, more maintainable code and reduced boilerplate.
Reduced Maintenance Overhead: Interface descriptions available at runtime allow for changes to interface definitions without updating corresponding type guards or validation logic, reducing maintenance overhead and minimizing inconsistencies.
What shortcomings exist with current approaches?
Duplicated code: Current approaches involve duplicating interface definitions, leading to redundancy.
Mistakes: Custom type guards may become outdated if not updated alongside interface changes.
Maintenance: While libraries like Zod allow type inference, it's not possible to infer schemas from TypeScript interfaces, leading to maintenance challenges.
What workarounds are you using in the meantime?
I define schemas using Zod with the "satisfies" keyword to ensure alignment with TypeScript interfaces.
The text was updated successfully, but these errors were encountered: