Suggestion: Allow Types in JS Files
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:12
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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
andcheckJs
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.