Mapping of branch names to corresponding schemas.
Input type for which schema can be read.
Convenience function to create a new variant value. Unlike the .from
it
has input typed so that type checker can help you to ensure that you are
providing all the required fields. If invalid input is provided, the
function will throw an error.
Function can be used to match the input against the variant schema to
return the matched branch name and corresponding value. It provides
convenience over standard .read
/ .from
methods by allowing user
to switch over the branch name as opposed to having to do nested if else
blocks.
Input to match against the variant schema.
Optional
fallback: ElseFall back value to return if the input does not match any of the branches. If not provided, the function will throw an error if the input does not match any of the branches.
const [kind, shape] = Shape.match(input)
switch (kind) {
case "circle": return `Circle with radius ${shape.radius}`
case "rectangle": return `Rectangle with width ${shape.width} and height ${shape.height}`
}
Generated using TypeDoc
Schema for the Variant types, which is a special kind of union type where every choice is a struct with a single field denoting the choice.
The
_
branch is a special case which can be used to represent all other choices that are not explicitly defined. Unlike other branches, the_
schema is passed the entire input instead of just the value of the field.Example