// This module contains utilities for converting signature types defined // in userspace into our internal representation of an invokable's // function type signature. import { NamedArgs, UnwrapNamedArgs } from './integration'; /** * Given an "args hash" (e.g. `{ Named: {...}; Positional: [...] }`), * returns a tuple type representing the parameters */ export type InvokableArgs = [ ...positional: Constrain, Array, []>, ...named: MaybeNamed>>, ]; /** Given a signature `S`, get back the normalized `Args` type. */ export type ComponentSignatureArgs = S extends { Args: infer Args; } ? Args extends { Named?: object; Positional?: unknown[]; } ? { Named: Get; Positional: Get; } : { Named: S['Args']; Positional: []; } : { Named: keyof S extends 'Args' | 'Blocks' | 'Element' ? {} : S; Positional: []; }; /** Given a signature `S`, get back the normalized `Blocks` type. */ export type ComponentSignatureBlocks = S extends { Blocks: infer Blocks } ? { [Block in keyof Blocks]: Blocks[Block] extends unknown[] ? { Params: { Positional: Blocks[Block] } } : Blocks[Block]; } : {}; /** Given a component signature `S`, get back the `Element` type. */ export type ComponentSignatureElement = S extends { Element: infer Element } ? NonNullable extends never ? unknown : Element : unknown; export type PrebindArgs> = NamedArgs< Omit, Args> & Partial, Args>> >; export type MaybeNamed = T extends any ? {} extends UnwrapNamedArgs ? keyof UnwrapNamedArgs extends never ? [] : [named?: T] : [named: T] : never; export type Get = K extends keyof T ? T[K] : Otherwise; export type Constrain = T extends Constraint ? T : Otherwise; export type TupleOfSize = Acc['length'] extends Len ? Acc : TupleOfSize; export type SliceTo = T['length'] extends Index ? T : T extends [...infer Rest, any?] ? SliceTo : []; export type SliceFrom = T extends [ ...TupleOfSize, ...infer Rest, ] ? Rest : [];