good solution, I further simplified your code
type IsEven<T extends number> = `${T}` extends `${number}${0|2|4|6|8}` ? T : never; type IsOdd<T extends number> = `${T}` extends `${number}${1|3|5|7|9}` ? T : never; function guarantyOddNumber<T extends number>(n: IsOdd<T>) { return n; } function guarantEvenNumber<T extends number>(n: IsEven<T>) { return n; } guarantyOddNumber(6876545); // succeed odd guarantyOddNumber(6876546); // fail odd guarantEvenNumber(6876546) // succeed even guarantEvenNumber(6876545); // fail even
however there is fundamental different between your solution and my original solution
yours can only be used in function, the original solution is intended to assign to a variable, for example
const a:OddNumber = 1
which is why it is a odd number type, not odd number check
I do however agree that the function one is more versatile
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
good solution, I further simplified your code
however there is fundamental different between your solution and my original solution
yours can only be used in function, the original solution is intended to assign to a variable, for example
which is why it is a odd number type, not odd number check
I do however agree that the function one is more versatile