Ts.SimpleSignature
Ts / SimpleSignature
Utilities for working with the __simpleSignature
phantom type pattern.
This pattern allows functions with complex generic signatures to provide a simpler signature for type inference in contexts like testing or documentation.
SimpleSignature
Import
import { Ts } from '@wollybeard/kit'
// Access via namespace
Ts.SimpleSignature.someFunction()
import * as Ts from '@wollybeard/kit/ts'
// Access via namespace
Ts.SimpleSignature.someFunction()
Functions
[F]
implement
<$Fn>(impl: GetSignature<$Fn>): $Fn
Parameters:
impl
- Implementation function typed with the simple signature
Returns: The implementation cast to the full function type
Helper to implement a function with a simple signature for inference.
This allows you to write the implementation using the simple signature types while the returned function has the full complex signature.
$Fn
- The full function interface (with complex generics)
Examples:
interface PartitionFn extends
SimpleSignature<[
(obj: object, keys: string[]) => { picked: object; omitted: object },
]>
{
<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): { picked: Pick<T, K>; omitted: Omit<T, K> }
}
export const partition = implement<PartitionFn>((obj, pickedKeys) => {
// Implementation typed with simple signature: object and string[]
return { picked: {}, omitted: { ...obj } }
})
Constants
[C]
symbol
typeof symbol
Utilities for working with the __simpleSignature
phantom type pattern.
This pattern allows functions with complex generic signatures to provide a simpler signature for type inference in contexts like testing or documentation.
SimpleSignature
Types
[I]
SimpleSignature
interface SimpleSignature<
$Overloads extends readonly [
(...args: any[]) => any,
...Array<(...args: any[]) => any>,
],
> {
[symbol]: $Overloads[number]
}
Helper interface for defining simple signatures with overloads.
Use this to define multiple overload signatures in a type-safe way. The type parameter accepts a tuple of function signatures.
$Overloads
- Tuple of function signature types
Examples:
interface MyFunctionType extends
SimpleSignature<[
(x: string) => number,
(x: number) => string,
(x: boolean) => boolean,
]>
{
// Your complex generic signature
<T extends string | number | boolean>(x: T): ComplexType<T>
}
// Single overload (most common case)
interface PartitionFn extends
SimpleSignature<[
(obj: object, keys: string[]) => { picked: object; omitted: object },
]>
{
<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): { picked: Pick<T, K>; omitted: Omit<T, K> }
}
[T]
GetSignature
type GetSignature<$fn> = $fn extends { [symbol]: infer $sig } ? $sig : $fn
Extract the signature from a function, preferring __simpleSignature
if available.
If the function has a __simpleSignature
property, returns that type. Otherwise, returns the function's actual type unchanged.
$fn
- The function type to extract from
Examples:
// Function without __simpleSignature
type Fn1 = (a: string, b: number) => boolean
type Result1 = GetSignature<Fn1> // (a: string, b: number) => boolean
// Function with __simpleSignature
declare const partition: {
<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): { picked: Pick<T, K>; omitted: Omit<T, K> }
[__simpleSignature]: (
obj: object,
keys: string[],
) => { picked: object; omitted: object }
}
type Result2 = GetSignature<typeof partition> // (obj: object, keys: string[]) => { picked: object; omitted: object }
[T]
GetParameters
type GetParameters<$fn> = GetSignature<$fn> extends (...args: any) => any
? Parameters<GetSignature<$fn>>
: never
Extract parameters from a function, using __simpleSignature
if available.
$fn
- The function type to extract parameters from
Examples:
type Params1 = GetParameters<(a: string, b: number) => void> // [a: string, b: number]
// With __simpleSignature
declare const partition: {
<T extends object, K extends keyof T>(obj: T, keys: K[]): any
[__simpleSignature]: (obj: object, keys: string[]) => any
}
type Params2 = GetParameters<typeof partition> // [obj: object, keys: string[]]
[T]
GetReturnType
type GetReturnType<$fn> = GetSignature<$fn> extends (...args: any) => any
? ReturnType<GetSignature<$fn>>
: never
Extract return type from a function, using __simpleSignature
if available.
$fn
- The function type to extract return type from
Examples:
type Return1 = GetReturnType<(a: string) => number> // number
// With __simpleSignature
declare const partition: {
<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): { picked: Pick<T, K>; omitted: Omit<T, K> }
[__simpleSignature]: (
obj: object,
keys: string[],
) => { picked: object; omitted: object }
}
type Return2 = GetReturnType<typeof partition> // { picked: object; omitted: object }