Recently I've been dedicated to enhancing my open-source library, but I've run into a puzzling issue that has me stumped. Let's take a look at the code snippet:
- In my setup, I have an array containing functions designed for transformations of strings.
type Transformation<T> = (value: T) => T
type Pipe<T> = Transformation<T>[]
- Additionally, I'm working with an array comprising two distinct yet interlinked types.
type SchemeOption<Value, Api> = [Constructor<Value>, () => Field<Value, Api>]
// The constructor and field types are structured as follows:
type Constructor<T> = <Arg>(...args: Arg[]) => T
type Field<T, Api> = (pipe: Pipe<T>) => Api
It's worth noting that both Constructor<Value>
and Field<Value, Api>
should share the same template argument type.
- I leverage this structure when defining the
extendScheme
function for expanding existing schemes.
type Scheme = {
[Key in string]: SchemeOption<any, any>
}
const extendScheme = <ParentScheme extends Scheme, ResultScheme extends Scheme>(
parent: ParentScheme,
second: (parent: ParentScheme) => ResultScheme
) => {
return second(parent)
}
However, a challenge arises when the Scheme
is established using SchemeOption<any, any>
, where the any
parameter may differ between two array elements:
// Expected behavior
extendScheme({}, () => ({
myCustomStringPipe: [
String,
() => (pipe: Pipe<string>) => ({
// ...
})
]
}))
// Undesirable outcome
extendScheme({}, () => ({
myCustomStringPipe: [
String,
() => (pipe: Pipe<number>) => ({ // modification of number type here
// ...
})
]
}))
Are there any suggestions on how to adjust the Scheme
or extendScheme
function to only accept arrays with consistent generic parameters?
To experiment with this issue, I have set up a Playground at: https://stackblitz.com/edit/typescript-uxbfsy?file=index.ts