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.

Suggestion: Allow Types in JS Files

See original GitHub issue

I have a large Angular2 project written in JavaScript, in which we use transform-flow-strip-types in order to allow Angular2 dependency injection to work from JavaScript.

This is currently an error in a JavaScript file compiled with tsc:

class MyClass {
  prop:string;                   //err: 'types' can only be used in a .ts file
  constructor(x: string) {       //err: 'types' can only be used in a .ts file
  }

  someMethod():string {        //err: 'types' can only be used in a .ts file
  }
}

I would like an allowTypesInJsFiles compiler option which would allow us to compile JavaScript files which contain types. All of the above errors would be valid if the flag is set. This would allow us to use Angular2 DI from JavaScript files.

I have a branch with a simple implementation which I have verified to work.

Is this a change which could be accepted? If so I will add tests and sign the CLA.

I think it would be very useful for people migrating large JavaScript projects to TypeScript.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:12
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
cfaestercommented, Jun 19, 2019

I hate to bump an old issue, however, as issues like #30115 spring up, and this one is still open, I want to ask what’s the status on this?

I believe adding what @sberan suggests would greatly support the adoption of TypeScript when you are only using tsc as a type-checker, and running the code through Babel. It would also be a big help when migrating large code bases.

2reactions
liamnesscommented, Oct 29, 2018

This seems like an old issue, and it may be that there is a resolution that I am missing, but if not I would like to request this feature addition. It’s a little confusing that allowJs and checkJs exist as options, but that you nonetheless must use a .ts extension to suppress the errors that result. Makes those options seem a little redundant. I am using many non-standard JS syntaxes and transforms in my projects, through Babel, but Typescript is the only one which has this requirement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure types in JavaScript file for getting auto ...
I'm trying to get auto-suggestion for a simple Button component (to get a suggestion of "red" or "blue"), similar to components in MUI....
Read more >
Working with JavaScript in Visual Studio Code
Type declaration files are written in TypeScript so they can express the data types of parameters and functions, allowing VS Code to provide...
Read more >
Documentation - Type Checking JavaScript Files - TypeScript
Here are some notable differences on how checking works in .js files compared to .ts files. Properties are inferred from assignments in class...
Read more >
Handbook - Type Checking JavaScript Files - TypeScript
In a .js file, types can often be inferred just like in .ts files. Likewise, when types can't be inferred, they can be...
Read more >
Type Hinting in JavaScript - StrongLoop
The first thing you need is a code editor that recognizes and supports the concept of types in JavaScript. You can either use...
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