1. 程式人生 > >[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5

[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5

tro wrong check this sta clas sudo ons assertion

Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service.

First of all, you should make sure you have [email protected] install:

sudo npm i -g [email protected]

Then add @ts-check to the top of js file:

// @ts-check

This tell typescript to check the file.

Check type assertion we can do:

// index.js

function
shouldBeNumber (/** @type {Number} */ num) { ‘use strict‘; console.log(num) }

In VScode, if will use the function by passing the wrong param, we will get error in IDE.

shouldBeNumber("100");

But this doesn‘t stop the compiler, the code will still be compiled without error.

[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5