typable
typable
is a mini library to include dynamic casting in TypeScript.
TypeRep<T>
Class new TypeRep<T>(uniqueName:string, …inheritance: TypeRep<any>[])
Creates a TypeRep
for type T
.
TypeRep<T>::is<R>(other: TypeRep<R>): this is TypeRep<T&R>
Detects whether this typeRep
is a sub-type-rep of other
.
TypeRep<T>::equal<R>(other: TypeRep<R>):this is TypeRep<T&R>
Detects whether this typeRep
is equivalent to other
.
Identical to typeRep.is(other) && other.is(typeRep)
.
Typable<T = {}>
Interface Typable<T>::dynamicCast<U>(tr: TypeRep<U>): undefined | U
This method is required for all Typable
s to implement: given a TypeRep<U>
, the object should return an instance of U
, if T
is a subtype of U
, or undefined
for other cases. TypeRep<T>::is
could be used in implementation.
implDynamicCast<U, T>(tr: TypeRep<U>, obj: Typable<T>, trObj: TypeRep<T>): undefined | U
Function This function is an utility function used to simplify implementation of dynamicCast
. Typical use is:
;
Sample
; ;;;;