xxxxxxxxxx
//parameter
type Parameter<T extends (args: any[]) => any> = T extends (args: infer P) => any ? P : never;
type paraMeterCheck = Parameter<(a: string, b: string) => void>;
xxxxxxxxxx
function createPerson(name: string, doAction: () => void): void {
console.log(`Hi, my name is ${name}.`);
doAction(); // doAction as a function parameter.
}
// Hi, my name is Bob.
// performs doAction which is waveHands function.
createPerson('Bob', waveHands());
xxxxxxxxxx
function doCoolStuff(a: string, b: number) {
// ...
}
type DoCoolStuffArgs = Parameters<typeof doCoolStuff> // [string, number]
type SecondArg = Parameters<typeof doCoolStuff>[1] // number
// demo: https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAEzgYTnANgZSiYYACgEMAuRAZygCcYwBzAGkQCMKwQBbVgUxoCUiAN4AoRIgD0kxADp5ogL6jRUAJ4AHXogAi6TLnyEAgjQaVEAXkQAFEjRJdeUfpQA86rXGAp92PATAAHxSMgDa1HSMLJw8-AC6KtKIAB6IABYkFp7aEbT0zIixfDTxLPTA-DS8yIjANHBciFDp2jkW3s2tiBr2js6uiJ2oGP5GwKIQCNSpZHqjhoGm5laIYQDk6TDrLACMAAyJoslqGVnNmtrF-OVglTTVtfWNXW2XQz4t2gBErN89fScLhoH18CwChEm0ygiDUZDsDiBrg8l2GfkWhCCYV28VWBySMhgMIA7nAaABrCxYGDktpbCznYm8LBYMiqd44XhTMDIZarBH9YHuHJo8HjLE4qFgGYALzInO5vLMeP2oiAA
xxxxxxxxxx
function add(a: number, b: string, c:boolean): string {
return a + b;
}
type AddReturnType = Parameters<typeof add>;
// type AddReturnType = [a: number, b: string, c:boolean];