-
-
Notifications
You must be signed in to change notification settings - Fork 59
the "is" keyword (type predicates) #227
Comments
Perhaps the following would have a type error: function isNumber(n: unknown): n is number {
if (typeof n === 'number') return true // Error, function returns true if n is `number | NaN`
return false
} because the type system can treat |
Seems like a lot of opportunity here for Hegel to prevent a lot of errors that TS doesn't! |
That would be super sweet to make |
We want to make it implicity. You can't set directly which type you want to refine. function isNumber(n: unknown) {
return typeof n === 'number' && !isNaN(n)
}
const a: number | boolean = 2;
if (isNumber(a)) {
// a will be number
} But the feature currently in development. So, I will note this issue in commits related to the feature. |
@JSMonk you should allow developer to specify what he/she wants to refine to so refinement itself can be checked against expectation of a developer for example function isNumber<T>(n: T): $Refine<T, number> {
return typeof n === 'string'// error
}
function isString<T>(n: T): $Refine<T, string> {
return typeof n === 'string'// ok
}
declare class Array {
static isArray<T>(T): $Refine<T, Array>
} of course inference of |
That's a good point. It could be possible to make a mistake in the return statement, and therefore by accident change the type guard that the boolean represents. |
To allow for type refinement to be abstracted into functions:
Try
The text was updated successfully, but these errors were encountered: