'What does the "percentage sign" mean in the Vue.js source code
I am reading Vue.js's source code(here) on github and stumbled upon something both I don't know and can't find an answer.
A related part from the source:
export function isObject (obj: mixed): boolean %checks {
return obj !== null && typeof obj === 'object'
}
What is the function or meaning of the "%"?
Solution 1:[1]
It's a Flow feature called Predicate Function.
In short, it denotes a function performing a type refinement.
In the specific example, it means that Flow knows that when a value is tested positively using isObject
, it will be of type object
.
Flow has some baked-in checks to refine a type, including things like obj !== null
and typeof obj === 'object'
, but if you extract it to a generic isObject
function returning boolean
, Flow won't trust that the function is performing a type refinement, unless you mark it with %checks
.
Note that TypeScript has a similar feature called User-Defined Type Guards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |