Issue #44  (Type-checking in JavaScript)02/22/23

Advertisement
Get Smarter Every Day
Every day Refind picks 5 links from around the web that make you smarter, tailored to your interests. Refind is a must-read newsletter loved by 100,000+ curious minds.

If you're not yet a TypeScript user, or if you use TypeScript but also work with some simpler JavaScript projects without TypeScript, there are a few features in VS Code you can take advantage of. These may improve your code and will help you catch mistakes and bad practices.

First, you can enable basic type checking for any JavaScript file by simply adding the line "// @ts-check" (without the quotes) to the top of any JavaScript file.
 
Type checking in JS files in VS Code
 
As shown in the screenshot above, when you enable type checking on a single file with the @ts-check directive, you'll get a red 'problem' warning in the file's tab, as well as the red underline on the problematic line. These aren't JavaScript errors, but they're treated like errors due to the type checking. In this instance the problem is trying to re-declare a variable as a number after it's already been initialized as a string. The message shown in the image is the warning that appears when you hover over the problem spot.

If you like this feature for all your native JavaScript files (i.e. .js files), you can enable it in your settings. Search for "tscheck" in your settings, then enable the option "JS/TS › Implicit Project Config: Check JS", which will appear as "js/ts.implicitProjectConfig.checkJs": true in your settings.json file.
 
Enabling type checking globally in VS Code

But again, you may not want the global setting. Using @ts-check, you can enable it just for one or two files, to see if it's something you want to use more extensively.

Similarly, you may have the global setting enabled to type check all .js files, but maybe you want to disable it for a few files. To disable type checking, add the line "// @ts-nocheck" to the top of any file.
 
Disabling type checking for individual files in VS Code

With this comment in place, now the file doesn't display any of the warnings it did before — even though type checking is enabled globally.

In some cases, there may be a particular JavaScript pattern that you're using that goes against the general type checking guidelines, but you still want to enable type checking for everything else. You can tell VS Code to ignore a specific warning by placing "// @ts-ignore" on the line above the error.
 
Ignoring a type error in VS Code

So once again, although type checking is enabled globally, this particular error is ignored because of the @ts-ignore directive.

Type checking has become pretty commonplace nowadays, so using the above features may help you write better and more maintainable JavaScript, even if you're not yet a TypeScript user.
 
Now on to this week's hand-picked links!
 
 

VS Code Tools

Swimm — A VS Code and JetBrains plugin that enables you to streamline internal code documentation and share developer knowledge across teams and repositories.

Databricks for Visual Studio Code — A VS Code extension, currently in preview, that allows you to develop for the Databricks Lakehouse platform (for data, analytics, and AI) directly in VS Code.

SpellBox — A versatile AI coding assistant for VS Code that creates the code you need from simple prompts, so you can easily solve your tough code problems.

Inline Parameters for VS Code — A VS Code extension that displays function parameter annotations inline when calling a function, with support for JS, React, TypeScript, PHP, and Lua.
 

VS Code Articles & Videos

Step-by-Step Guide to Using MongoDB Locally with VS Code for Efficient Development — This goes through installing the MongoDB extension, connecting to a database, managing your databases, and the benefits of doing so using VS Code.

▶ VS Code Tips — Maximizing Editor Groups — A neat little video quick tip for working with multiple editors in VS Code.

Become a Master with React.js — A premium training course consisting of 70+ HD videos across 11 course modules, to learn to build modern, full-stack React and GraphQL apps.   Sponsor 

Customizing VSCode for CDK: A Guide to Creating Custom Tasks — This might be a useful tutorial for those working with the AWS Cloud Development Kit (or CDK).
 
 

Best of the Rest

Why Vim is More Than Just an Editor – Vim Language, Motions, and Modes Explained — The author, who's used a number of different editors and IDEs, makes the case that Vim, despite the steeper learning curve, becomes a much faster editor over time compared to its competition.

RunCode — A Cloud developer environment and workspace that's simple, secure, ready to use, and fully configurable, with support for a number of Linux-based programming languages and frameworks.

Aurora Editor — An IDE built by the community, for the community, and written entirely in Swift for lightning-fast performance and a great feel on macOS.


Suggestions?

If you have any link suggestions, including a tool, article, or other resources related to VS Code or another IDE, send it via DM on X: @LouisLazaris or just hit reply on this email.

That's it for this issue.

Happy VS Coding!
Louis
VSCode.Email
@LouisLazaris
Copyright © VSCode.Email. All rights reserved.

Not affiliated with Microsoft, Visual Studio Code, or any of its trademarks.