Coding⏱️ 2 min read📅 2026-06-03

How to Fix: Error: 'types' can only be used in a .ts file - Visual Studio Code using @ts-check

Error in TypeScript configuration file. The @ts-check directive is used to enable type checking for a specific file, but it should be placed at the top of a .ts file, not a .js file.

Quick Answer: Move the @ts-check directive to the top of your .ts file and remove the plugin installation, as TSLint is not compatible with @ts-check. Instead, use the built-in TypeScript linting features or install a different linting tool.

Error: 'types' can only be used in a .ts file - Visual Studio Code using @ts-check

The error message indicates that the '@ts-check' directive is being used in a non-.ts file, which is not allowed.

🛑 Root Causes of the Error

  • The issue arises from attempting to enable TypeScript checking on a JavaScript file without specifying a .ts extension.
  • This can be resolved by renaming the file to have a .ts extension or using the 'files.withTsConfig' setting in your VS Code settings to opt-in to TypeScript checking for all files.

🛠️ Step-by-Step Verified Fixes

Renaming the file to include a .ts extension

  1. Step 1: Rename the file from .js to .ts to indicate that it should be treated as a TypeScript file.
  2. Step 2: Update any references to 'module.exports' to 'export function name'.

Enabling files.withTsConfig setting

  1. Step 1: Open the Command Palette in VS Code and type 'Settings: Open Settings (JSON)' to edit the settings.
  2. Step 2: Add the following line to the JSON file under the "editor" section: "files.withTsConfig": true,"

🎯 Final Words

By making these changes, you can enable TypeScript checking on your JavaScript file and take advantage of linting features like TSLint.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions