Type check JavaScript files using JSDoc and Typescript 2.5

Akash Gutha
InstructorAkash Gutha
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

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

[00:00] We'll go ahead and make a function and call it strictlyNumbers. We'll pass in a number, and inside this, we'll log it. I call this function, passing in a string. If we go ahead and run that file in Node, we can see the JavaScript runs this function without throwing any errors.

[00:25] That is because JavaScript neither does type checking nor has an embedded type system. TypeScript augments this exact functionality by including JS doctype assertion support for JavaScript files inside the TS check service.

[00:38] Once we import the TS check service, we will be able to write JS doctype assertions in front of the parameters and enable type checking. We'll write a direct type, and in the curly braces, we'll pass it in the type number.

[00:52] We can quickly see that VS code throws an error on the function. There it says, "Argument of type 100 is not assignable to parameter of type number." You can see that we have enforced type checking in JavaScript.

[01:06] We can quickly fix this by changing this into a number. You can see that that error is gone. If we change this back into a string, the error is reproduced.

[01:15] View mode is a suggested service that TypeScript provides us to enforce type checking in JavaScript files, but this doesn't actually affect the compilation process. If we go ahead and run this in our terminal or Node, it will work perfectly fine.

egghead
egghead
~ 45 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today