Imagine I have a generic TypeScript function that looks like this:
function performAction<X>(input: X): X {
//...
}
Now, let's consider a specific interface called YourType
:
interface YourType {
a: string;
b: number;
}
I aim to export the performAction
method with the assumption that the generic parameter X
will always be YourType
. This way, when I type something like:
performAction({
...the editor will automatically suggest completion for properties a
and b
, eliminating the need for me to specify X
beforehand.
One approach is to:
import {performAction as _performAction} from 'some-package';
export const performAction = _performAction as (input: YourType) => YourType;
However, this method is tedious and prone to errors. Moreover, the actual performAction
has more elements and variations than shown in this simplified illustration, which would require extensive copying and pasting.
Is there a simpler way to "fill in" X
without duplicating the entire original definition?