TanStack

IfTypeParameter

ts
type IfTypeParameter<T, TThen, TElse> = [TypeParameterProbe] extends [HotkeyPartsOf<T> | StepsOf<HotkeyPartsOf<T>>] ? TThen : IfGenericHotkey<T, TThen, TElse>;

Defined in: hotkey.types.ts:488

TThen while the hotkey type T, one of its entries, alternates, or sequence steps is still generic: a type parameter, or a type built from one, such as Shortcuts[K], as in a wrapper around a hotkey function, and TElse once T is a hotkey type.

A function that checks its hotkey with a conditional type, such as KnownHotkey, can't check a type parameter: the condition stays unresolved, and TypeScript rejects the call. With this type the function takes TThen in a wrapper and keeps its check everywhere else, in one signature. TypeScript relates an argument to an unresolved conditional type by its branches, and skips the false branch when the checked type is assignable to the extends type for any T. The probe is.

Type Parameters

T

T

TThen

TThen

TElse

TElse

Example

ts
function useHotkey<const T extends RegisterableHotkey>(
  hotkey: IfTypeParameter<T, RegisterableHotkey, KnownHotkey<T>>,
): void

// Compiles: `T` is a type parameter here.
function useShortcut<T extends Hotkey>(hotkey: T) {
  useHotkey(hotkey)
}