Roine

Typescript - Create type definition from object

February 06, 2020

You can generate a type from an object

// base file file.ts
const myObj = {a: 1, b: 2}

in the shell

tsc --declaration file.ts

it’ll generate two files, the compile js file and the type declaration file.

/// type declaration file file.d.ts
declare const myObj: {
    a: number;
    b: number;
};

The JS file can be ignored