From 62598ec5cdfab941f2dc8d4147a36ae674eee9a5 Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Fri, 26 Jul 2024 10:10:02 +0200 Subject: [PATCH] Describe how TypeScript checks JavaScript files (#6238) --- .../101-ts-js-interoperability.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/data/roadmaps/typescript/content/100-typescript/101-ts-js-interoperability.md b/src/data/roadmaps/typescript/content/100-typescript/101-ts-js-interoperability.md index 7fd2e6fd1..5ebc49286 100644 --- a/src/data/roadmaps/typescript/content/100-typescript/101-ts-js-interoperability.md +++ b/src/data/roadmaps/typescript/content/100-typescript/101-ts-js-interoperability.md @@ -5,3 +5,24 @@ TypeScript and JavaScript have full interoperability, meaning you can use TypeSc You can use JavaScript libraries in TypeScript projects by either including the JavaScript files directly or using type definitions for the library. Type definitions provide type information for JavaScript libraries, making it easier to use them in TypeScript. On the other hand, you can use TypeScript code in JavaScript projects by simply compiling the TypeScript code into JavaScript. The generated JavaScript code can be used in any JavaScript environment, and it will work the same way as regular JavaScript code. + +TypeScript's compiler also supports type checking for plain JavaScript code by adding the `// @ts-check` comment at the top of a file. This allows the compiler to validate types by inspecting the JSDoc comments: + +```typescript +// @ts-check + +/** + * Adds two numbers together. + * @param {number} a - The first number. + * @param {number} b - The second number. + * @returns {number} The sum of the two numbers. + */ +function add(a, b) { + return a + b; +} +``` + +Learn more from the following links: + +- [@article@Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) +- [@video@Using JavaScript in TypeScript](https://youtu.be/AZhZlEbBaB4)