How to use javascript in typescript

You have two main options.

Option 1: Add JS to your TS compilation context

Simply set allowJs to true in your tsconfig.json compilerOptions and then make sure the .js file is included using files/include/exclude etc.

Option 2: Declare your JS for use in your TS

e.g. if you are going to call a js function anExampleFunction you can simply create a declaration (in a .d.ts file):

declare const anExampleFunction: Function;

And now you will be able to call the function from TypeScript.

More

  • Some tips on migrating from JS to TS: https://basarat.gitbook.io/typescript/type-system/migrating
  • More on declaration files: https://basarat.gitbook.io/typescript/type-system/intro

Leave a Comment

techhipbettruvabetnorabahisbahis forumu