Can you use TypeScript and JavaScript together?

Yes, you can use TypeScript and JavaScript together in the same project. TypeScript is a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. This makes it easy to migrate an existing JavaScript project to TypeScript bit by bit.

To use TypeScript and JavaScript together, you can simply add TypeScript files to your project alongside your existing JavaScript files. TypeScript files have the .tsx’ extension, while JavaScript files have the .js’ extension. TypeScript files can import and use JavaScript files without any issues, since TypeScript is designed to be compatible with JavaScript.

 

Suppose you want to use TypeScript features in your JavaScript code. In that case, you can gradually convert your JavaScript files to TypeScript by renaming them with the .tsx extension and adding TypeScript syntax and type annotations as needed. TypeScript also provides a –allowJs’ flag that allows you to use JavaScript files in a TypeScript project without any modifications.

 

It’s worth noting that if you’re using a JavaScript library or framework that doesn’t have TypeScript typings available, you won’t get the benefit of TypeScript’s static typing and other features for that library or framework. However, you can still use TypeScript for the rest of your codebase and gradually add typings as needed.

 

The best ways of combining TypeScript and JavaScript.

TypeScript and JavaScript can be used together in a variety of ways. Here are some examples of the best ways to combine them:

  • Gradual migration:

Suppose you have an existing JavaScript file, and you want to start adding TypeScript gradually to it. Here’s an example of how you can do that:

// existing JavaScript code

function greet(name) {

  console.log("Hello, " + name + "!");

}




greet("John");

 

You can start adding TypeScript by renaming the file to .tsx’ and adding a type annotation to the name parameter:

// TypeScript code

function greet(name: string) {

  console.log("Hello, " + name + "!");

}




greet("John");

 

The TypeScript compiler will compile this code to JavaScript, and you can gradually add more type annotations to the code as needed.

  • Third-party libraries:

Suppose you want to use the popular moment.js library in your TypeScript project. You can install the library via npm:

npm install moment

 

Then, create a file moment.d.tsx’ in your project’s types’ directory (or any directory with a tsconfig.json’ file) and add the following code to it:

// moment.d.tsx

declare module "moment" {

  function moment(): any;

  export = moment;

}

 

This tells TypeScript that the “moment” module exists and exports a single function, and that it can be imported using the standard ES6 module syntax:

// TypeScript code

import * as moment from "moment";




const now = moment();

console.log(now.format("YYYY-MM-DD"));

  • Type augmentation:

Suppose you’re using the Axios library in your TypeScript project, but it doesn’t have type definitions. You can create a file axios.d.tsx’ and add the following code to it:

// axios.d.tsx

import * as axios from "axios";




declare module "axios" {

  export interface AxiosRequestConfig {

    timeout?: number;

  }

}

This adds a timeout’ property to the AxiosRequestConfig’ interface. Now you can use timeout’ in your Axios requests:

// TypeScript code

import axios from "axios";




const response = await axios.get("https://api.example.com", {

  timeout: 5000,

});

  • JavaScript modules in TypeScript:

Suppose you have a JavaScript module math.js’, that exports a function:

// math.js

function add(a, b) {

  return a + b;

}




module.exports = {

  add: add,

};

You can import this module into your TypeScript file like this:

// TypeScript code

import { add } from "./math";




const sum = add(2, 3);

console.log(sum); // 5

  • Using TypeScript in JavaScript projects:

Suppose you have a JavaScript project, but you want to use TypeScript for some files. You can install TypeScript via npm:

npm install typescript

 

Then, create a tsconfig.json’ file in your project’s root directory and add the following configuration:

{

  "compilerOptions": {

    "target": "es6",

    "module": "commonjs",

    "outDir": "dist"

  },

  "include": ["src/**/*.tsx"],

  "exclude": ["node_modules"]

}

 

This tells TypeScript to compile TypeScript files in the src’ directory and output them to the dist’ directory. Now you can start writing TypeScript files in the src’ directory and have them compiled to JavaScript automatically. For example:

// TypeScript code

function

Overall, using TypeScript and JavaScript together can provide a lot of benefits, such as improved code quality and better tooling support. By combining them in the right way, you can get the best of both worlds.

About the Author: Ekaterina Eremeeva

Share This Post, Choose Your Platform!

Request a Consultation