interface Operator<Value> {
id: string;
isApplicable: (value: Value) => boolean;
apply: (value: Value) => void;
}
interface Operation {
uid: string;
operatorId: string;
}
const operators = new Map<string, Operator<unknown>>([
[
'add',
{
id: 'add',
isApplicable: isSpectrum1D,
apply: (spectrum: Spectrum) => {
cast<Spectrum1D>();
spectrum.foo += 1;
},
},
]
]);
function run(value: unknown, operations: Operation[]) {
for (const operation of operations) {
const operator = operators.get(operation.operatorId);
assertDefined(operator);
assert(operator.isApplicable(value));
operator.apply(value);
}
}
Casts the value to the Output type. This kind of helpers should be avoided whenever it is possible. It is roughly equivalent to
as unknown as Outputcast. There is not checks. Use it when you are sure that the value is of the Output type, but for some reason TypeScript is not able to infer it, and for optimization purpose you want to avoid the redundant checks.