Skip to content

noImplicitAny from parent tsconfig.json cannot be overridden #55983

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

Closed
digeomel opened this issue Oct 4, 2023 · 8 comments
Closed

noImplicitAny from parent tsconfig.json cannot be overridden #55983

digeomel opened this issue Oct 4, 2023 · 8 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@digeomel
Copy link

digeomel commented Oct 4, 2023

🔎 Search Terms

noImplicitAny

🕗 Version & Regression Information

  • I was unable to test this on prior versions because I never needed to set noImplicitAny to true before. The TypeScript version used in my example is 5.1.3.

⏯ Playground Link

No response

💻 Code

https://github.com/digeomel/angular-no-implicit-any

🙁 Actual behavior

I have an Angular 15 workspace with a main app and multiple independent projects (aka libraries). For the main app and some projects, I want to set noImplicitAny: true for the TypeScript compiler, but for some others, I want it to remain false (the default). The tsconfig.lib.json files inside the libraries are extending the main tsconfig.json from the root Angular folder (although I tried removing the extends as well, no difference, read below).

The problem is, no matter what I do, I keep getting errors about noImplicitAny from the libraries where it is even explicitly set to false! It seems that the TypeScript compiler disregards the individual tsconfig.lib.json settings inside those libraries and takes the global setting.

Moreover, I tried moving the setting from the main tsconfig.json in the root folder, to the app-specific src/tsconfig.app.json and I still get the same error from the libraries!

These are the other contents of the main tsconfig.json file:

{
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "module": "es2022",
        "target": "es2022",
        "lib": ["es2022", "dom"],
        "forceConsistentCasingInFileNames": true,
        "noImplicitOverride": true,
        "noPropertyAccessFromIndexSignature": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "moduleResolution": "node",
        "emitDecoratorMetadata": false,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "importHelpers": true,
        "typeRoots": ["node_modules/@types"],
        "resolveJsonModule": true,
    },
    "angularCompilerOptions": {
        "enableI18nLegacyMessageIdFormat": false,
        "strictInjectionParameters": true,
        "strictInputAccessModifiers": true,
        "strictTemplates": true
    }
}

We are not using "full" TypeScript strict mode.

What am I missing?

🙂 Expected behavior

I expect that the TypeScript compiler will respect the individual project settings and overrides. In my example above, the test2 library has this tsconfig.lib.json:

{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
        "outDir": "../../out-tsc/lib",
        "declaration": true,
        "declarationMap": true,
        "inlineSources": true,
        "noImplicitAny": false,
        "types": []
    },
    "exclude": ["**/*.spec.ts"]
}

So, noImplicitAny is set to false, however I'm getting an error in test2.component.ts:

image

Additional information about the issue

Not sure if this is about the Angular compiler or the TypeScript compiler, if you think it's Angular, please close the issue and I will post it in the Angular repo issues.

I also posted this on stackoverflow, with no luck, that's why I'm trying here as well:
https://stackoverflow.com/questions/77214574/angular-tsconfig-cannot-use-different-noimplicitany-settings-for-different-proje

@fatcerberus
Copy link

"noImplicitAny": false

😱

@RyanCavanaugh
Copy link
Member

I'm not sure what the repro is supposed to be showing. When I run tsc, I get an implicit any error. This is expected because "noImplicitAny": true is set in tsconfig.json. What is the way to repro the "problem" ?

@RyanCavanaugh
Copy link
Member

It seems like maybe you're expecting that if a file from the root tsc imports a file which has a tsconfig.json in its directory spine, then that tsconfig should take precedence over the invoked tsconfig's settings, but that's not how it works. Settings are computed based on the tsconfig that is being built; two files in the same tsc invocation will not get different configuration.

@fatcerberus
Copy link

fatcerberus commented Oct 5, 2023

... which has a tsconfig.json in its directory spine ...

This is genius. I've been looking for a nice, succinct term for "...in any directory whose path is a substring of this given one" for years. "...in the directory spine of" describes that perfectly.

@digeomel
Copy link
Author

digeomel commented Oct 5, 2023

@RyanCavanaugh there is a main tsconfig.json on the root path, but there are also individual tsconfig.lib.json and tsconfig.app.json files in the individual project folders. As I wrote above, in the tsconfig.lib.json file of the test2 project, I explicitly set noImplicitAny to false, so I expect the compiler to honor this, on all files under that directory.

And to reiterate, I am not sure this is a TypeScript or an Angular issue, because I'm not invoking the TypeScript compiler directly, I'm invoking the Angular cli/compiler.

@fatcerberus
Copy link

in the tsconfig.lib.json file of the test2 project, I explicitly set noImplicitAny to false, so I expect the compiler to honor this, on all files under that directory

Right, and as Ryan explained, it doesn’t work that way. When you invoke tsc, you invoke it on one particular tsconfig at a time, and all source files that are part of that run, whether through include or imported, are compiled using the settings in that config. Think of a tsconfig as a project file, not as a “settings for this directory” file.

What you’re trying to do requires either composite or project references (I don’t remember which it was), so that the files with different settings get compiled separately and imports of them will use their generated .d.ts files instead of pulling in the TS source directly.

@digeomel
Copy link
Author

digeomel commented Oct 5, 2023

@fatcerberus thanks for the explanation @fatcerberus , sorry but I didn't get it the way @RyanCavanaugh explained it.

Is it possible that the Angular compiler (ngc) invokes tsc internally with separate processes, respecting the individual settings? Do you think I might have better luck posting the issue there?

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 5, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants