convert
Create a TypeObject that converts values to the given type.
type.convert(Type)
Given a type, returns a TypeObject that will coerce values to that type.
import { ObservableObject, type } from "can";
class Event extends ObservableObject {
static props = {
date: type.convert(Date)
};
}
let event = new Event({
date: new Date()
});
console.log(event.date); // -> Date
event.date = "12/14/1933";
console.log(event.date); // -> Date{12/14/1933}
Parameters
- Type
{function}
:A constructor function that values will be checked against.
Use Case
Use type.convert to build types for models, such as those with can-rest-model and can-realtime-rest-model. You often want to use convert because the database where data is stored will serialize types. This is true of of the Date type which is often serialized as strings. It's also true of subtypes (like other ObservableObjects) which are serialized as plain objects.