resolve any number of arguments

This commit is contained in:
Jos de Jong 2023-09-14 14:51:49 +02:00
parent 11792e0435
commit 722ee05a0a

View File

@ -36,21 +36,21 @@ function createFactory<BaseTypes extends Record<string, unknown>>() {
type ResolveType<T extends K> = BaseTypes[T] type ResolveType<T extends K> = BaseTypes[T]
type ResolveTuple<T> = type ResolveArguments<S extends string> =
T extends K ? [ResolveType<T>] : S extends '' ? [] :
T extends `${infer L extends K}, ${infer M extends K}` ? [ResolveType<L>, ResolveType<M>] : S extends `${infer T extends K}, ${infer U}`
T extends `${infer L extends K}, ${infer M extends K}, ${infer N extends K}` ? [ResolveType<T>, ...ResolveArguments<U>]
? [ResolveType<L>, ResolveType<M>, ResolveType<N>] : S extends `${infer T extends K}` ? [ResolveType<T>]
: never : never;
type ResolveFunction<T> = type ResolveFunctionDef<T> =
T extends `${infer Name}(${infer Args}) : ${infer L extends K}` T extends `${infer Name}(${infer Args}) : ${infer L extends K}`
? (...args: ResolveTuple<Args>) => ResolveType<L> ? (...args: ResolveArguments<Args>) => ResolveType<L>
: never : never
return function create<T extends string, U extends string, V>( return function create<T extends string, U extends string, V>(
dependencies: { [L in T]: U }, dependencies: { [L in T]: U },
callback: (deps: { [L in T]: ResolveFunction<U> }) => V callback: (deps: { [L in T]: ResolveFunctionDef<U> }) => V
) { ) {
return callback return callback
} }