Seeking advice on how to effectively utilize TypeScript for strongly typing a function that operates similarly to the following example:
function createDeserializer(typeDeserializers) {
return (data) => {
const deserializer = typeDeserializers[data.__type];
<br>return deserializer(data);
}
}
I would like to achieve something along these lines:
const personDeserializer = createDeserializer({
'employee': (data) => new Employee(data),
'customer': (data) => new Customer(data)
});
const person1 = personDeserializer({ __type: 'employee', ... });
const person2 = personDeserializer({ __type: 'customer', ... });
// TypeScript should recognize that person1 is an 'Employee' instance, and person2 is a 'Customer' instance.
I believe I may need to implement some kind of key mapping within the typeDeserializers
property and somehow relate it to the return value, but I'm somewhat unsure of how to proceed.