Skip to content

NthParameter

Since 1.1.0

Get the Nth parameter type (0-indexed).

Signature

typescript
type NthParameter<T, N extends number> = T extends (...args: infer P) => any ? P[N] : never

Parameters

ParameterDescription
TThe function type
NThe parameter index (0-indexed)

Examples

Basic Usage

typescript
import type { NthParameter } from 'uni-types'

type Fn = (a: string, b: number, c: boolean) => void

type First = NthParameter<Fn, 0>   // string
type Second = NthParameter<Fn, 1>  // number
type Third = NthParameter<Fn, 2>   // boolean

Extract Specific Parameters

typescript
type FetchFn = (url: string, options: RequestInit, callback: () => void) => void

type Url = NthParameter<FetchFn, 0>      // string
type Options = NthParameter<FetchFn, 1>  // RequestInit
type Callback = NthParameter<FetchFn, 2> // () => void

Released under the MIT License.