Relative Content

Tag Archive for typescripttypescript-generics

Cannot infer argument or return type from callback dictionary without explicit generics using TypeScript

I store data in objects with different versions that use different keys and different types. The acceptable types for the data properties are defined in an interface that defines the versions as a dictionary type. I want to match the object with handler functions based on a matching key in the data. I create a dictionary value of handlers that stores the functions indexed by the associated key. I have a generic handle function that takes one of the data objects, a dictionary of all handlers, and a callback that receives a matching value and handler function.

Cannot consistently infer return union type from generic callback array with TypeScript

I have a function createUnion that takes an array of callbacks with different return types and returns one of the results. I want it to return a union of the return types. It works perfectly when combining types with null or undefined, but not other types. They are not even done in order precedence. How can I correctly infer the union type from all the callbacks?