I was trying to implement a generic function that accepts an interface of functions and data, and passes the results from one another.
Inference is broken, any help will be appreciated.
Link to a CodeSandbox of the Code That Does Not Compile
function InitViewModel<S, C, M>(params: {
state: S;
computed: (s: S) => C;
methods: (s: S, c: C) => M;
}) {
const state = params.state;
const computed = params.computed(state);
const methods = params.methods(state, computed);
return {
state,
computed,
methods
};
}
export const VM = InitViewModel({
state: { message: "This Will Be Infered As expected" },
computed: (state /* infered */) => ({
computedMessage: state.message + " But This Will Not"
}),
methods: (state /* infered */, computed /* inferred wrong */) => {
return {
logName: () => console.log(state.message),
logComputedName: () => console.log(computed.computedMessage) // Does not compile
};
}
});
computed.computedMessagedoes not exist becausecomputedis a function. Returned value havecomputedMessagesocomputed(someStatus).computedMessageshould have more sense. But I didn't understand what are you trying to do.Cinferred correctly. Might be a limitation in the way these two features work. Maybe someone else has some idea..