Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkJS has no effect when allowJS is not enabled in the tsconfig.json #21435

Closed
egamma opened this issue Jan 27, 2018 · 4 comments
Closed

checkJS has no effect when allowJS is not enabled in the tsconfig.json #21435

egamma opened this issue Jan 27, 2018 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@egamma
Copy link
Member

egamma commented Jan 27, 2018

Background we want to enable checkJS in a folder that contains JS code inside the vscode repository microsoft/vscode#41953. This folder is a hybrid TS and JS folder and would be a good application for enabling checkJS.

However, setting checkJS to true without setting allowJS to true in a tsconfig.json file has no effect:

checking happens when both attributes are true:
image

no checking when allowJS is set to false:
image

Defining allowJS to true for this folder has the side effect that the compiler wants to emit transpiled JS files and would overwrite the existing JS files. Having to change the project structure to use an output folder should not be required when enabling checkJS and I haven´t found a way to specify noEmit only for JS files.

Therefore, it must be possible to check JS files using the project context defined in the tsconfig.json without that the JS files are transpiled and code is emitted.

We have tried to work around this by adding //@ts-check comments to the JS files. However, this has the side effect that the project context defined in the tsconfig.json is ignored. This results in nasty issues that a typings file is used from the global type cache but not from a typings file that is included in a child folder of the tsconfig.json folder. This results in false errors.

Related #16738 but this issue proposes to automatically enable allowJS when checkJS is enabled. The issue here is about enabling checkJS without transpiling the JS files and generating code.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 30, 2018

--checkJs really just mean report errors in .js files already included in the project. The flag is needed since the default behavior for .js files is that they do not report any errors, and we did not want to introduce a breaking change when this feature was added.

And thus --checkJs really means nothing without --allowJs. As you noted #16738 has a proposal of making --checkJs mean both include and report errors. I think that is a fair thing to do to avoid the unnecessary duplication..

For the OP, the original design for --allowJs is you have .js files that may also need to be transpiled. e.g. has a class or asnyc. and in that sense, the .js files are jsut treated like other .ts files and processed more or less the same way.

I believe what you are requesting here is a new mode where you can tell the compiler what files need to be transpiled, and what files only need to be checked only and do not bother moving them to the output... I think using --allowJs fails --checkJs true to mean that is very subtle, and we are better off having a diffrent flag/setting mechanism.

Currently the two modes available are, transpile all files, including .js files (--allowJs), or check-only all files, including .ts/.js files (--allowJs --noEmit).

For a check-only context, you need to create a new project. this can be done by simply adding a new tsconfig.json file that inherits the configurations from your main one and adds .js files, and --noEmit.

I would like to get more context on what is it that you were trying to accomplish, and how common is this hybrid projects (with .ts and .js files) that require checking but not transpiling the .js files.

@egamma
Copy link
Member Author

egamma commented Jan 31, 2018

I would like to get more context on what is it that you were trying to accomplish, and how common is this hybrid projects (with .ts and .js files) that require checking but not transpiling the .js files.

In VS Code we have a folder https://github.com/Microsoft/vscode/tree/master/build that contains the gulp build scripts and some helpers, these are all .js files. We do not want to have a build/transpile step for the build scripts, for this reason we kept them as .js files. This folder also happens to contain some build related code like our packager which is implemented in Typescript. The scenario is that we would like to get type checking for the .js files and I was hoping to enable type checking with minimal changes to the project configuration.

Having to define another project context/tsconfig.json should work, but I was hoping for a simpler way to achieve this.

@egamma
Copy link
Member Author

egamma commented Jan 31, 2018

I was able to get the desired setup using two separate tsconfig.json files as @mhegazy suggested. While this works it looks a bit contrived.

tsconfig.json - used by the language service

{
	"compilerOptions": {
		"target": "es5",
		"module": "commonjs",
		"noImplicitAny": false,
		"removeComments": false,
		"preserveConstEnums": true,
		"sourceMap": false,
		"experimentalDecorators": true,
		"newLine": "LF",
		"allowJs": true,
		"checkJs": true
	},
	"exclude": [
		"node_modules/**"
	]
}

tsconfig.build.json - used by the compiler/build

{
	"extends": "./tsconfig.json",
	"compilerOptions": {
		"allowJs": false,
		"checkJs": false
	}
}

@mhegazy mhegazy added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 1, 2018
@RyanCavanaugh
Copy link
Member

Fixed at #40275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants