Ad hoc number coercion
const NumberLike: FromLike<Like<number, string | boolean>> = {
fromLike(value: number | string | boolean): number {
return Number(value);
},
};
NumberLike.fromLike("42") // yields 42
NumberLike.fromLike(true) // yields 1
Using an ad hoc conversion with a @Property fastener
class Person {
@Property({valueType: NumberLike})
readonly age!: Property<Like<number, string | boolean>>;
}
const person = new Person();
person.age.setValue("42");
person.age.value // yields 42
Generated using TypeDoc
Associates a type
Twith a loosely typed representationL. Loose type representations are modeled as a phantom methodlikeType?(like: L): voidwhose argument type is the loosely typed representation ofT. Because thelikeTypemethod is optional, values of typeTare assignable to typeLike<T, L>, and vice versa.The preferred way to associate loose type representations with a custom class is to declare a
likeType?(like: L): voidmethod on the class itself. TheLiketype is used to inject a phantomlikeTypemethod into an existing type. This enables ad hoc loose types to be declared without needing to change the declaration of the strict type.