1

Is there any way to generate Typescript definitions from a NodeJS/Javascript file ?

I have file which looks like :

module.exports = {
  account_a: {
    name: 'a',
    title: 'Account a',
  },
  account_n: {
    name: 'n',
    title: 'Account n',
  },
  // and so on
};

and would like to generate some Typescript definitions of it.

Actually, I'm copy/pasting its content to a TS file and compile it using tsc --declaration . but it's not ideal, any idea ?

  • Possible duplicate of [Generating typescript declaration files from javascript](https://stackoverflow.com/questions/18301898/generating-typescript-declaration-files-from-javascript) – Heretic Monkey Nov 22 '19 at 16:37
  • I tried the solutions provided in this topic but it's not really what I am trying to achieve, I don't want to generate a .ts file, only its defintions – Léo Georges Nov 22 '19 at 16:46
  • It generates a .d.ts file, which is a definition file. – Heretic Monkey Nov 22 '19 at 16:49
  • 1
    Does this answer your question? [How do you produce a .d.ts "typings" definition file from an existing JavaScript library?](https://stackoverflow.com/questions/12687779/how-do-you-produce-a-d-ts-typings-definition-file-from-an-existing-javascript) – jaspenlind Nov 22 '19 at 22:23
  • Sounds like `tsc --allowJs --declaration file.js` is what I'm looking for, thank you – Léo Georges Nov 28 '19 at 14:16

1 Answers1

0

actually you can using in TypeScript this below code

**exports** = {
      account_a: {
        name: 'a',
        title: 'Account a',
      },
      account_n: {
        name: 'n',
        title: 'Account n',
      },
      // and so on
    };
Schart
  • 56
  • 1
  • 3