check
Create a strictly typed TypeObject.
type.check(Type)
Given a type, returns a TypeObject that will check values against that type. Throws if another type is provided as a value.
The following creates an object with a strongly typed Number so that any other value cannot be passed to it.
import { ObservableObject, type } from "can";
class Pagination extends ObservableObject {
static props = {
num: type.check(Number)
};
}
let page = new Pagination({ num: 1 });
console.log(page.num); // -> 1
page.num = 2;
console.log(page.num); // -> 2
page.num = "4";
// throws for providing an invalid type.
Parameters
Use Case
Use type.check to create strongly typed properties. This is useful when used with StacheElement components. Strongly typed properties helps to ensure that the component works correctly because invalid types cannot creep in and cause unexpected behavior.
Using strongly typed properties with can-type sends a singal to users of the component that the component needs these properties in a certain type.