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
A currently functional example of this motivating example is
typeAtleastOneLengthArray=[string, ...string[]];
The limitation of which is that if you were to use it like..
constfunc=(input: AtLeastOneLengthArray)=>{}constarray=["one","two"]func(array)// -> Argument of type 'string[]' is not assignable to parameter of type '[string, ...string[]]'
More generally this is useful when defining a value as a number, and if you require that it be less than some number
functionassertsAtLeastFive=(input: number): asserts number is >5{if(input<=5)throw"error";}
which has uses when you're doing math manipulation and allows for the requirement of asserting values.
When defining complex typescript functions it's sometimes useful to be able to compare types or even add them. This is particularly useful for recursive methods. To do this comparison currently you need to do something like this..
typeFortyFive='0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|end'// e.g. "0|1|2" -> ["0", "1|2"]typeDecompose<S>=Sextends `${infer FirstNumber}|${infer RestOfSet}` ? [FirstNumber,RestOfSet] : never;typeToString<T>=Textendsnumber ? `${T}` : never;// max for LessThanThis is 45, ToBeCompared can be any integertypeLessThan<ToBeComparedextendsnumber|string,LessThanThisextendsnumber|string,Current=FortyFive>=ToString<ToBeCompared>extendsDecompose<Current>[0]
? ToString<ToBeCompared>extendsToString<LessThanThis>
? false
: true
: ToString<LessThanThis>extendsDecompose<Current>[0]
? ToString<LessThanThis>extendsDecompose<Current>[0]
? false
: true
: Decompose<Current>extendsnever
? false
: LessThan<ToBeCompared,LessThanThis,Decompose<Current>[1]>// max for GreaterThanThis is 45, ToBeCompared can be any integertypeGreaterThan<ToBeComparedextendsnumber|string,GreaterThanThisextendsnumber|string>=LessThan<GreaterThanThis,ToBeCompared>// is 1 greater than 2?typeTest1=GreaterThan<1,2>// -> false// is 2 greater than 2?typeTest2=GreaterThan<2,2>// -> false// is 3 greater than 2?typeTest3=GreaterThan<3,2>// -> true// is 2 less than 1?typeTest4=LessThan<2,1>// -> false// is 1 less than 1?typeTest5=LessThan<1,1>// -> false// is 0 less than 1?typeTest6=LessThan<0,1>// -> true
But it would be nicer to instead do
// is 1 greater than 2?typeTest1=1>2// -> false// is 2 greater than 2?typeTest2=2>2// -> false// is 3 greater than 2?typeTest3=3>2// -> true// is 2 less than 1?typeTest4=2<1// -> false// is 1 less than 1?typeTest5=1<1// -> false// is 0 less than 1?typeTest6=0<1// -> true
which would be useful, for example
typeGetArrayElement<Array,Index,Current=0>=CurrentextendsIndex ? Array[index] : GetArrayElement<Array,Index,Current+1>typeexample=GetArrayElement<["one","two","three"],1>// example -> "two"
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
greater than
less than
equal to
integer
math
adding numbers
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
The ability to compare integers as type declarations. Currently you can do so with strings but not numbers, which would be very useful.
π Motivating Example
I'm not sure what the best syntax would be but something like
The motivating example was me trying to define an array with at least 1 element
π» Use Cases
A currently functional example of this motivating example is
The limitation of which is that if you were to use it like..
More generally this is useful when defining a value as a number, and if you require that it be less than some number
which has uses when you're doing math manipulation and allows for the requirement of asserting values.
When defining complex typescript functions it's sometimes useful to be able to compare types or even add them. This is particularly useful for recursive methods. To do this comparison currently you need to do something like this..
But it would be nicer to instead do
which would be useful, for example
The text was updated successfully, but these errors were encountered: