FirstParameter type
Reads the type of the first parameter of a function, such as the event of a handler.
ts
import type { FirstParameter } from 'utils';Signature
ts
export type FirstParameter<F extends (...arguments_: readonly never[]) => unknown> = Parameters<F>[0];Parameters
| Name | Description |
|---|---|
F | The function type. |
Example
ts
type Event = FirstParameter<typeof onResize>; // ResizeObserverEntryTests
Line coverage: 0 %.
- ✅ reads the first parameter
Source
src/types/first-parameter.ts (on GitHub)
Source code
ts
/**
* Reads the type of the first parameter of a function, such as the event of a handler.
*
* @template F - The function type.
* @example
* type Event = FirstParameter<typeof onResize>; // ResizeObserverEntry
*/
export type FirstParameter<F extends (...arguments_: readonly never[]) => unknown> = Parameters<F>[0];Test file
ts
import type { FirstParameter } from './first-parameter';
describe('FirstParameter', () => {
it('reads the first parameter', () => {
expectTypeOf<FirstParameter<(event: MouseEvent, index: number) => void>>().toEqualTypeOf<MouseEvent>();
expectTypeOf<FirstParameter<() => void>>().toEqualTypeOf<undefined>();
});
});Keywords: first, parameter, types, type