convertAll
Derive a new type that makes every property of Type convert, eliminating strict type checking.
type.convertAll(Type)
Create a new type that inherits the same properties of Type
, but where each property on the Type
is converted, rather than being strictly checked.
import { ObservableObject, Reflect, type } from "can/everything";
class Person extends ObservableObject {
static props = {
age: Number
}
}
// Age is a strict number.
let person = new Person();
try {
person.age = "13";
} catch(err) {
console.log("Oops, tried to convert a strict type.");
}
let ConvertingPerson = type.convertAll(Person);
person = Reflect.new(Person);
person.age = "13";
console.log("Age is", person.age); // logs 13
Parameters
- Type
{function}
:Any type with a getSchema implementation.
Use with fixture.store
This function is useful to create derived types where any strict types are ignored. Useful in cases where you might receive strings for each key.
One example is using store, which will provide string values in some cases. type.convertAll
can be used to create a type where this works.
import { fixture, ObservableObject, type } from "can/everything";
class Person extends ObservableObject {
static props = {
age: Number
};
}
const items = [{ id: 1, age: "13"}];
fixture.store(items, type.convertAll(Person));