Last updated: Feb 29, 2024
Reading timeยท5 min
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
.
Here is an example of how the error occurs in a file called 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.
.js
fileOne way to solve the error is to remove the TypeScript typings.
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.
// โ๏ธ 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.
// โ Pure JavaScript code function getObj() { return { name: 'Bobby Hadz', age: 30 }; } const getObj2 = () => { return { name: 'Bobby Hadz', age: 30 }; };
Here is another example.
// โ๏ธ code with TypeScript typings const arr: { name: string; age: number }[] = [ { name: 'Alice', age: 27 }, ];
And here is the JavaScript equivalent of the TypeScript code.
// โ Pure JavaScript code const arr = [ { name: 'Alice', age: 27 }, ];
Here is one last example of removing TypeScript typings from a .js
file.
// โ๏ธ 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.
// โ 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.
.ts
or .tsx
extensionAnother 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.
const arr: { name: string; age: number }[] = [ { name: 'Alice', age: 27 }, ];
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
.
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;
The syntax is valid because the file has a .tsx
extension (and not .js
).
.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.
An alternative way to suppress the error by disabling the JavaScript Validate setting.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
Type javascript validate and hit Enter
.
Uncheck the checkbox of the JavaScript Validate: Enable setting to disable it.
.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.
settings.json
fileAn alternative approach to disable the JavaSCript Validate setting is to do it directly in your settings.json file.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
settings.json
file.{ "javascript.validate.enable": false, }
Make sure to remove the trailing comma if the property comes last.
Once you disable the setting, the error will be suppressed.
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.
In the root directory of your project, create a .vscode
folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "javascript.validate.enable": false }
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.
You can learn more about the related topics by checking out the following tutorials: