Skip to content

Invoking TypeScript with --outDir src makes some source files invisible #60867

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
jthemphill opened this issue Dec 29, 2024 · 3 comments
Closed

Comments

@jthemphill
Copy link

🔎 Search Terms

outDir globalThis declarations exclude include not found error

TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature

🕗 Version & Regression Information

⏯ Playground Link

https://github.com/jthemphill/ts-5.5-repro

💻 Code

Run with tsc --outDir src.

tsconfig.json:

{
  "compilerOptions": {
    "noEmit": true,
    "noImplicitAny": true
  },
  "files": ["src/test.spec.ts"],
  "include": ["./src/**/*"]
}

src/test.spec.ts:

globalThis.__IS_CI__ = true;

common.ts:

declare global {
  namespace globalThis {
    var __IS_CI__: boolean;
  }
}
export {};

🙁 Actual behavior

On TS 5.4, the command passes.

On TS 5.5 or newer, the command fails with the error:

src/test.spec.ts:5:12 - error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

5 globalThis.__IS_CI__ = true;

🙂 Expected behavior

I expect both commands to pass.

Additional information about the issue

  • I'm trying to migrate a large codebase off of Typescript 5.3, and this is the last blocker remaining.
  • --outDir src is passed to tsc via a Bazel build rule.
  • If {"outDir": "src"} is added to tsconfig.json, then typechecking will fail on both versions of TypeScript.

As I understand it, according to #58327, outDir causes source files to be excluded because otherwise, we'll overwrite the .d.ts files we're using as source files.

But if you don't specify outDir at all, your output directory will be the same as your source directory anyway, and somehow this won't be a problem. According to the TypeScript docs:

If [outDir is] not specified, .js files will be emitted in the same directory as the .ts files they were generated from.

Since we already have default behavior where the source and output directories are the same, why does specifying outDir cause such a different behavior?

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 29, 2024

See: https://www.typescriptlang.org/tsconfig/#exclude

Unless explicitly specified, exclude includes outDir as a default value. If you don't specify outDir then it has no value (since it has no default value) and is not part of exclude, that's why you see different behaviors.

@jthemphill
Copy link
Author

Ah, thank you! I may ask to add a link from outDir to exclude in the docs so that future mes are less confused🙂

@jthemphill
Copy link
Author

This is the correct answer to my question!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants