ยท hands on

JavaScript in TypeScript: Learn How to Use Them Together

TypeScript is a superset of JavaScript, so you can use regular JavaScript code in a TypeScript application. This means you can start with your existing JavaScript codebase and gradually migrate it to TypeScript. You can also use legacy JavaScript libraries and frameworks without any issues.

It is possible to use regular JavaScript code in a TypeScript application. TypeScript is a superset of JavaScript, which means that any valid JavaScript code is also valid TypeScript code. This allows developers to gradually adopt TypeScript by starting with their existing JavaScript codebase and gradually migrating it to TypeScript. Additionally, developers can use legacy JavaScript libraries and frameworks in a TypeScript application without any issues.

Contents

Video Tutorial

The following tutorial shows you how to learn TypeScript step by step by importing your JavaScript code into a TypeScript project.

Use JavaScript Libraries

When working with legacy JavaScript code, you may encounter cases where you need to use a global variable that was previously injected by JavaScript code. In such situations, TypeScript offers a solution through the use of the special keyword declare. This keyword allows for declaring types for your existing JavaScript code and libraries to utilize them within your TypeScript codebase. However, it's important to keep in mind that even though TypeScript offers additional type checking and other features, the final code will still be compiled to JavaScript and executed within a JavaScript runtime environment.

Example:

declare function myGlobalLegacyFunction(input: number): void;
 
myGlobalLegacyFunction(1337);

Though not recommended practice, you can also declare external namespaces with any:

declare const myNamespace: any;
 
console.log(myNamespace.name);
Back to Blog