-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to determine whether an unknown variable is a specific Record / Tuple #388
Comments
Records are anonymous primitive compound value, use a structural check (test fields and types) instead of a nominal check ( Your example would be: function handlePoint(x) {
if (isPoint(x)) {
console.log(`x is a point:`, x)
} else {
throw new Error(`x is not a point`)
}
}
function isPoint(x) {
return typeof x === "record" && typeof x.x === "number" && typeof x.y === "number"
} There are many runtime or compile time libraries to help with type checking, this is out of scope for this proposal in my opinion. The Pattern Matching proposal may be a better place to discuss such feature. |
Having an "is same structure" predicate could be interesting, but the exact semantics would be tricky. For example, taking First, should such a predicate recurse into values that are R/T? Aka, should it check the structure of Comparing structure deeply by type of the leaf values might be a good compromise. Then you could implement the |
This is still type checking, but the type is defined with a "template" value. This feels like a needless extra indirection when you could have The main issue is what you described: the semantics are not clear cut. Answering the semantics means defining a type system for JavaScript (beyond TC39 is very far from being ready to standardize on a type system that can be checked IMO. Type Annotations is related when going in this direction. Reserving type syntax for custom use means that a standard type system would have to coexist with it, making it a bit harder to get. |
try to name
#{ x, y }
:or:
The text was updated successfully, but these errors were encountered: