I know how to define a custom type predicate:
type UnsignedInteger = number;
function isUnsigned (s: number): s is UnsignedInteger {
return s > -1
}
But how can I obtain such an error if I try to assign an invalid number?
const a: UnsignedInteger = -1 // Compiler error: cannot assign...
A type predicate does not change the type.
Currently, the functionality you require is not available in Typescript unless if you list out every possible number, like the following: