As I attempted to create a versatile function that can accept an interface containing both functions and data, there seems to be an issue with inference. Assistance in resolving this problem would be greatly appreciated.
To view the code that is failing to compile, please visit this link to the CodeSandbox.
function InitializeModel<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 myModel = InitializeModel({
state: { message: "Expecting Correct Inference Here" },
computed: (state /* inference should work */) => ({
computedMessage: state.message + "But This Won't"
}),
methods: (state /* inference should work */, computed /* this one was inferred incorrectly */) => {
return {
logName: () => console.log(state.message),
logComputedName: () => console.log(computed.computedMessage) // Compilation Error
};
}
});