GoncaloNGT
GoncaloNGT

Reputation: 465

How to retrieve type of function parameter externally?

I have a typed object with a function like this:

export interface Example {
  edit<T extends Animal>(animal: T | Mammal<T>): void;
}

How can I access the type of the function parameter externally?
I've tried:

import { Example } from './not-relevant';

type AnimalParam = Example['edit'](0); // This should be equal to Animal | Mammal<Animal>

Note: The generics are not the main point of the question, but I've included them in this example since I'm using it in my code.

Upvotes: 0

Views: 54

Answers (1)

Romain TAILLANDIER
Romain TAILLANDIER

Reputation: 2015

If you need to get the parameter type of the function, take a look at Parameters : https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype

type AnimalParam = Parameters<Example['edit']>[0];

Parameters<Example['edit']> is a tuple of all parameters of the function, and take the first with [0].

Upvotes: 1

Related Questions