I'm building a function that accepts a functions chain, where each function transforms results from the previous one. In the given example, _context
should be equal { hello: 'world', something: 'else' }
:
function withWorld<Context extends {}>(context: Context) {
return { ...context, hello: 'world' }
}
function withSomething<Context extends {}>(context: Context) {
return { ...context, something: 'else' }
}
test([withWorld, withSomething], (_context) => {})
However, when I try to describe this behavior with generics, I stumble upon an error:
No overload matches this call:
Types of parameters 'contextSomething' and 'contextB' are incompatible.
Type 'unknown' is not assignable to type '{}'.
Here's the code (TypeScript playground):
export interface TestFunction {
<A>(
conditions: [(contextSolo: {}) => A],
body: (contextBodySolo: A) => any
): any
<A, B>(
conditions: [(contextA: {}) => A, (contextB: A) => B],
body: (contextBody: B) => void
): any
}
const test: TestFunction = () => void 0
function withWorld<Context extends {}>(contextWithWorld: Context) {
return { ...context, hello: 'world' }
}
function withSomething<Context extends {}>(contextSomething: Context) {
return { ...context, something: 'else' }
}
test([withSomething], (_context) => {})
test([withWorld], (_context) => {})
test([withWorld, withSomething], (_context) => {})
When there's a single function in the array, TypeScript infers the types alright, even when the example is more complicated and has an initial state (TypeScript playground).
Complete error:
No overload matches this call.
Overload 1 of 2, '(conditions: [(contextSolo: {}) => any], body: (contextBodySolo: any) => any): any', gave the following error.
Argument of type '[<Context extends {}>(contextWithWorld: Context) => any, <Context extends {}>(contextSomething: Context) => any]' is not assignable to parameter of type '[(contextSolo: {}) => any]'.
Source has 2 element(s) but target allows only 1.
Overload 2 of 2, '(conditions: [(contextA: {}) => unknown, (contextB: unknown) => any], body: (contextBody: any) => void): any', gave the following error.
Type '<Context extends {}>(contextSomething: Context) => any' is not assignable to type '(contextB: unknown) => any'.
Types of parameters 'contextSomething' and 'contextB' are incompatible.
Type 'unknown' is not assignable to type '{}'.(2769)