Type annotations can only be used in TypeScript files [Fix]

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
5 min

banner

# Table of Contents

  1. Type annotations can only be used in TypeScript files
  2. Removing the TypeScript typings from your .js file
  3. Renaming your file to use a .ts or .tsx extension
  4. Disabling JavaScript validation to suppress the error
  5. Disabling JavaScript validation in your settings.json file
  6. Disabling JavaScript validation only in the current project

# Type annotations can only be used in TypeScript files

The VS Code error "Type annotations can only be used in TypeScript files" occurs when you use TypeScript typings in a file that has a .js extension.

To solve the error, remove the typings from your JavaScript file or change the extension of the file to .ts or .tsx.

type annotations can only be used in typescript files

Here is an example of how the error occurs in a file called App.js.

App.js
import React from 'react'; const App = () => { // ๐Ÿ‘‡๏ธ TypeScript typings const handleClick = (event: React.MouseEvent<HTMLElement>) => { console.log(event.target); console.log(event.currentTarget); }; return ( <div> <button onClick={handleClick}>Click</button> </div> ); }; export default App;

The file has a .js extension and contains TypeScript typings.

# Removing the TypeScript typings from your .js file

One way to solve the error is to remove the TypeScript typings.

App.js
import React from 'react'; const App = () => { // ๐Ÿ‘‡๏ธ removed typings const handleClick = (event) => { console.log(event.target); console.log(event.currentTarget); }; return ( <div> <button onClick={handleClick}>Click</button> </div> ); }; export default App;

Here are some other examples of removing TypeScript typings from .js code.

This is the TypeScript version of the code.

index.js
// โ›”๏ธ code with TypeScript typings function getObj(): { name: string; age: number } { return { name: 'Bobby Hadz', age: 30 }; } const getObj2 = (): { name: string; age: number } => { return { name: 'Bobby Hadz', age: 30 }; };

And here is the JavaScript version of the code.

index.js
// โœ… Pure JavaScript code function getObj() { return { name: 'Bobby Hadz', age: 30 }; } const getObj2 = () => { return { name: 'Bobby Hadz', age: 30 }; };

Here is another example.

index.js
// โ›”๏ธ code with TypeScript typings const arr: { name: string; age: number }[] = [ { name: 'Alice', age: 27 }, ];

And here is the JavaScript equivalent of the TypeScript code.

index.js
// โœ… Pure JavaScript code const arr = [ { name: 'Alice', age: 27 }, ];

Here is one last example of removing TypeScript typings from a .js file.

index.js
// โ›”๏ธ code with TypeScript typings interface Employee { id: number; name?: string; salary?: number; } const emp: Employee = { id: 1, name: 'Bobby Hadz', salary: 100, }; const name: string = emp.name as string; // ๐Ÿ‘ˆ๏ธ type assertion console.log(name); // ๐Ÿ‘‰๏ธ "Bobby Hadz"

And here is the equivalent JavaScript code.

index.js
// โœ… Pure JavaScript code const emp = { id: 1, name: 'Bobby Hadz', salary: 100, }; const name = emp.name; console.log(name); // ๐Ÿ‘‰๏ธ "Bobby Hadz"

Once you remove all TypeScript typings from the .js file, the error will be resolved.

# Renaming your file to use a .ts or .tsx extension

Another way to solve the error is to rename your file to use a .ts or .tsx extension.

If you write pure TypeScript code and your project supports TypeScript files, then you should use the .ts extension.

For example, the following code is perfectly valid and causes no issues.

index.ts
const arr: { name: string; age: number }[] = [ { name: 'Alice', age: 27 }, ];

set file extension to ts

The code is stored in an index.ts file. Notice that the extension is .ts (and not .js).

I've written a detailed guide on how to rename files in VS Code.

If you write React.js code, then you should use the .tsx extension.

Here is an example of a file called App.tsx.

App.tsx
import React from 'react'; const App = () => { const handleClick = (event: React.MouseEvent<HTMLElement>) => { console.log(event.target); console.log(event.currentTarget); }; return ( <div> <button onClick={handleClick}>Click</button> </div> ); }; export default App;

set file extension to tsx

The code for this article is available on GitHub

The syntax is valid because the file has a .tsx extension (and not .js).

Note that all of your React files in which you write JSX code (tags) and contain TypeScript typings should have a .tsx extension.

I've written a detailed guide on how to use Create React App with TypeScript if you'd like to read more on the topic.

If the issue persists even after renaming your file to have a .ts or .tsx (React) extension, restart your VS Code instance.

# Disabling JavaScript validation to suppress the error

An alternative way to suppress the error by disabling the JavaScript Validate setting.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type javascript validate and hit Enter.

  2. Uncheck the checkbox of the JavaScript Validate: Enable setting to disable it.

disable javascript validate setting

  1. Switch back to the .js file and the error should not be shown anymore.

The setting basically disables the built-in VS Code validation, so VS Code won't show the red underline when errors are encountered.

# Disabling JavaScript validation in your settings.json file

An alternative approach to disable the JavaSCript Validate setting is to do it directly in your settings.json file.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following line to your settings.json file.
settings.json
{ "javascript.validate.enable": false, }

Make sure to remove the trailing comma if the property comes last.

disable javascript validation in settings json

Once you disable the setting, the error will be suppressed.

# Disabling JavaScript validation only in the current project

You can also disable JavaScript validation only for the current project.

This is useful when you only encounter the issue in your current project but you still want to take advantage of using the built-in VS Code validation features in other projects.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "javascript.validate.enable": false }

disable javascript validation only in current project

Note that the properties in your .vscode/settings.json file only apply to the current project and override any global configuration.

If the error persists, restart your VS Code window.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev