Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Parameters<Type> #11

Open
nmsn opened this issue Dec 7, 2022 · 1 comment
Open

Parameters<Type> #11

nmsn opened this issue Dec 7, 2022 · 1 comment

Comments

@nmsn
Copy link
Contributor

nmsn commented Dec 7, 2022

Constructs a tuple type from the types used in the parameters of a function type Type.

declare function f1(arg: { a: number; b: string }): void;
 
type T0 = Parameters<() => string>;
// type T0 = []

type T1 = Parameters<(s: string) => void>;
// type T1 = [s: string]

type T2 = Parameters<<T>(arg: T) => T>;
// type T2 = [arg: unknown]

type T3 = Parameters<typeof f1>
// type T3 = [arg: {
//     a: number;
//     b: string;
// }]

type T4 = Parameters<any>;  
// type T4 = unknown[]

type T5 = Parameters<never>;
// type T5 = never

type T6 = Parameters<string>;
// Type 'string' does not satisfy the constraint '(...args: any) => any'.  
// type T6 = never

type T7 = Parameters<Function>;
// Type 'Function' does not satisfy the constraint '(...args: any) => any'.
// Type 'Function' provides no match for the signature '(...args: any): any'.
// type T7 = never
@nmsn nmsn added the TypeScript label Dec 7, 2022
@nmsn
Copy link
Contributor Author

nmsn commented Dec 7, 2022

/**
 * Obtain the parameters of a function type in a tuple
 */
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant