ts-cast
Version:
Runtime typechecking
12 lines (11 loc) • 535 B
JavaScript
import { casterApi } from '../engine';
import { throwTypeError } from '../engine/throw-type-error';
export const ref = (casterRef, typeName = '<type-reference>') => {
let caster = (value, context, reportError) => {
caster = casterRef();
return caster(value, context, reportError);
};
const referredCaster = (value, context, reportError = throwTypeError) => caster(value, context, reportError);
Object.defineProperty(referredCaster, 'name', { value: typeName });
return casterApi(referredCaster);
};