/** * Ensures that all instances of [prototype] have their property [key] mapped by [converter]. */ function makePropertyConverter(prototype: any, key: string, converter: (value: any) => any) { const values = new Map(); Object.defineProperty(prototype, key, { set(firstValue: any) { Object.defineProperty(this, key, { get() { return values.get(this); }, set(value: any) { values.set(this, converter(value)); }, enumerable: true, }); this[key] = firstValue; }, enumerable: true, configurable: true, }); } /** * Decorator that will take an Angular `@Input` and ensure that the value passed is converted to the proper type. */ export function InputConverter(converter: (value: any) => any) { return (target: Object, key: string) => { makePropertyConverter(target, key, converter); }; }