Auto generate index.d.ts, type definitions, from a typescript module

altus picture altus · Jun 5, 2017 · Viewed 15.3k times · Source

If I have a TypeScript module saved as my-function.ts as follows :

export function myFunction (param: number): number { return param }

This will be compiled to JavaScript in whichever way and loose its type definitions. I am then able to create a index.d.ts file which declare this module's definitions, but this seems a bit tedious to redefine/redeclare the definitions.

Are there ways to generate the type definitions automatically from the my-function.ts file to a index.d.ts file?

Answer

Daniel Rosenwasser picture Daniel Rosenwasser · Jun 5, 2017

If you compile with the --declaration flag, TypeScript will automatically generate .d.ts files for you.

This mode will require that you certain types are visible so that they can be described in your .d.ts files.