You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support to create type with custom detection function in TS
Use Cases
I hope to strengthen the type system of TS,
To meet more complex requirements (such as creating a string type with limited length), and simplify code operation.
(there is a drawback and that can be made up:)
The high uncertainty brought by the custom detection function (especially the unknown performance overhead required to execute the function during type checking).
(this can be achieved by limiting the custom detection function to: type will be further checked when typescript is compiled to JavaScript, and only type structure existing in the current version will be checked when writing code; to maintain the experience when writing code)
It may take a lot of work to realize it.
And I have a gradual proposal, which can completely avoid the above shortcomings, but also castrate the function I proposed:
Instead of adding a custom detection function to a compiler or type check, you can simply add an optional "validation function" property to the type, and then you can reference its validation function while referencing the type
Examples
//old
type T_Second = number
//new
supertype T_Second = (p:number)=>{
return (p > 0 && p < 60)
}
/*
let s = somefn() //s = 2
T_Second testfn(s) // true
*/
Supertype receives a function whose parameter is unique and must indicate the type (the function parameter is the basic type), and the return value of this function can only be Boolean (used to further verify the type)
Checklist
My suggestion meets these guidelines:
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, etc.)
@RyanCavanaugh I suddenly have an accurate implementation method, as follows:
supertype MyNumber (p:number):boolean{
return (p > 0 && p < 60)
}
//type MyNumber = number | Error
//use example
function use_example(num:MyNumber):MyNumber{
return num-1
}
let i = use_example(0) as (Error | number)
convert to current ts:
type__supertype_MyNumber=numberfunctionMyNumber(p:number):number|Error{function_(p:number):boolean{return(p>0&&p<60)}return_(p)===true ? p : newError("supertype MyNumber returned false")}//use examplefunctionuse_example(__supertype_num:number):number|Error{letnum=MyNumber(num)returnMyNumber(num-1)}leti=use_example(0)as(Error|number)
Search Terms
enhanced type system
Suggestion
Support to create type with custom detection function in TS
Use Cases
I hope to strengthen the type system of TS,
To meet more complex requirements (such as creating a string type with limited length), and simplify code operation.
(there is a drawback and that can be made up:)
The high uncertainty brought by the custom detection function (especially the unknown performance overhead required to execute the function during type checking).
(this can be achieved by limiting the custom detection function to: type will be further checked when typescript is compiled to JavaScript, and only type structure existing in the current version will be checked when writing code; to maintain the experience when writing code)
It may take a lot of work to realize it.
And I have a gradual proposal, which can completely avoid the above shortcomings, but also castrate the function I proposed:
Instead of adding a custom detection function to a compiler or type check, you can simply add an optional "validation function" property to the type, and then you can reference its validation function while referencing the type
Examples
Supertype receives a function whose parameter is unique and must indicate the type (the function parameter is the basic type), and the return value of this function can only be Boolean (used to further verify the type)
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: