question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to disable TS8010: 'types' can only be used in a .ts file.

See original GitHub issue

Search Terms

TS8010 TS 8010 types can only be used in a .ts file ‘types’ can only be used in a .ts file

Suggestion

Ability to disable TS8010: ‘types’ can only be used in a .ts file. Type annotations could simply be ignore by tsc when compiling a .js file.

Use Cases

I work in a JS / TS blended codebase. My team uses WebStorm as our IDE, and it supports type annotations in .js files. When a variable/param is typed, it provides type-specific autocompletion. As you can imagine, that’s quite powerful when working in a JS environment. Consequently, we have thousands of type annotations throughout our JS files. We would like to add allowJs:true to our tsconfig.json, however tsc throws an error for every type annotation in each JS file.

Checklist

My suggestion meets these guidelines:

  • [ x ] This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • [ x ] This wouldn’t change the runtime behavior of existing JavaScript code
  • [ x ] This could be implemented without emitting different JS based on the types of the expressions
  • [ x ] This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ x ] This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:22
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

26reactions
alexkrolickcommented, Dec 21, 2020

I am in a similar boat with Flow => TS migration. The ideal configuration during the migration is checkJs: false, allowJs: true, so that we can import JS files (with Flow types) into the TS files. But tsc give me thousands of errors for Type annotations can only be used in TypeScript files when it tries to see if the JS files contain TS code and mistakes the Flow annotations for TS. We can compile the code using Webpack, but can’t do a static check in CI, with this setup.

12reactions
ValentinHcommented, Apr 9, 2020

This could also be a valid use case for me. Indeed, I’m migrating an app from Flow to Typescript.

Right now, I’m using allowJs: true in my config and it enables me to import JS modules written in Flow into a TS modle with a working type-check in VSCode. However, if I run tsc --noEmit to ensure that my ts files are valid, I get tons of TS8010 errors.

Being able to disable this during the migration would really help us.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: 'types' can only be used in a .ts file - Visual Studio ...
Go to Extensions tab; Search for @builtin TypeScript and JavaScript Language Features; Click on Disable. Share.
Read more >
how to solve Type annotations can only be used in TypeScript
This video is a solution for a problem caused in react native and react TypeScript. This problem generally occurs while creating ...
Read more >
“Type annotations can only be used in TypeScript files.ts ...
one solution can be to disable the typescript check. so inside the settings.json add the below code line "javascript.validate.enable": false.
Read more >
Static Type Checking
Static type checkers like Flow and TypeScript identify certain types of problems before you ... It is developed at Facebook and is often...
Read more >
Typescript: 'type aliases' can only be used in a .ts file.
you must be using type aliases in .js file. To disable compiler errors in javascript, make sure that "allowJs" is set to false...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found