Get Function Parameters and Return Types with Typescript

This is a note titled "2019-04-17-get-return-and-params-typescript" taken from my note Library. Return to Library.

When creating advanced functions in Typescript, it can be difficult to get everything to work without resorting to using any.

Working with functions is definitely one of the hardest things, luckily Typescript has a bunch of useful helpers to save the day.

import { doACoolThing } from 'cool-lib';

// get the type of the imported function
type CoolThingFunction = typeof doACoolThing;

function sayAndDoSomething(
    text: string,
    // set the "coolThing" to be the first param
    // of the imported function
    coolThing: Parameters<CoolThingFunction>[0]
    // set the return type to be the return type
    // of the imported function
): ReturnType<CoolThingFunction>  {
    sayText(text);
    return doACoolThing(coolThing);
}
Bennett Hardwick

Bennett is a Software Developer working at Clipchamp. He spends most of his day playing with React and Gatsby, and editing videos. He's not a fan of social media, but you can follow him on Github