Create TypeScript Declarations from JavaScript and JSDoc

Create TypeScript Declarations from JavaScript and JSDoc

TypeScript allows you to generate type declaration files directly from JavaScript files and will use information in JSDoc comments to assign the appropriate types. Today’s smart code editors, like Visual Studio Code, are able to read type information from JSDoc and provide Intellisense options as you code.

Even if you don't write your code in TypeScript you can still generate .d.ts files to provide more information to developer tools.

For a variety of reasons, I prefer to write my code in vanilla JavaScript with JSDoc comments rather than writing in TypeScript. Today’s smart code editors, like Visual Studio Code, are able to read type information from JSDoc and provide Intellisense options as you code. However, those consuming your code may not get the same benefit unless you include a TypeScript declaration file along with the source files. Fortunately, TypeScript allows you to generate type declaration files directly from JavaScript files and will use information in JSDoc comments to assign the appropriate types.

First, make sure you have TypeScript installed:

$ npm install typescript --save-dev

Then, create a tsconfig.json file with the following options:

{
    "include": [
        "**/*.js"
    ],
    "compilerOptions": {
        "declaration": true,
        "emitDeclarationOnly": true,
        "allowJs": true
    }
}

Make sure that include specifies the files you want to generate declaration files for. The compilerOptions are:

  • declaration - generate the declarations for the files specified
  • emitDeclarationOnly - only output .d.ts files and not source files
  • allowJs - expect the source files to be JavaScript

javascript typescript programming developer web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

TypeScript vs. JavaScript Comparison – Pros, Cons, Trends

We'll look at the pros, cons and trends of TypeScript compared to JavaScript. What has driven so many developers away from JavaScript and into TypeScript? Let’s make a side-by-side TypeScript vs JavaScript comparison.