Closed
Description
π Search Terms
typescript, inference, decorators, assertions, constraints
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
I would like to request a feature involving the use of one or more special decorators for providing even more information about types, functions, and properties.
π Motivating Example
@constraint((obj: Foo) => { assert(obj.x > obj.y) })
@constraint((obj: Foo) => { assert(obj.y >= 0) })
@constraint((obj: Foo) => { assert(obj.x >= 0) })
class Foo
{
x: number;
y: number;
}
@constraint((obj: ExtendsFoo) => { assert(obj.z >= 0) })
class ExtendsFoo extends Foo
{
z: number;
}
π» Use Cases
-
What do you want to use this for? Even more granular expressiveness about types. functions, and properties would be useful.
-
What shortcomings exist with current approaches? With one or more special decorators, developers could express many more useful assertions and constraints.
-
What workarounds are you using in the meantime? Workarounds are situation-dependent.