quick-lint / quick-lint-js

quick-lint-js finds bugs in JavaScript programs
https://quick-lint-js.com
GNU General Public License v3.0
1.52k stars 192 forks source link

declares globals and exports in .d.ts file not recognized #1172

Open KraXen72 opened 8 months ago

KraXen72 commented 8 months ago

sveltekit projects have a app.d.ts file. i use it like this:

declare global {
    interface NavLink {
        type: 'nav',
        title: string,
        href: string,
    }
    interface SubmenuSeparator {
        type: 'separator'
    }
}
// ...
export {};

However, the global types are not recognized and show: image

tsconfig:

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
        "target": "es2021"
    }
    // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
    //
    // If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
    // from the referenced tsconfig.json - TypeScript does not merge them in
}

the "./.svelte-kit/tsconfig.json" is auto-generated / changes a lot, better to check a bare sveltekit starter project.

vegerot commented 8 months ago

I think the documentation needs to be improved.

  1. https://quick-lint-js.com/errors/E0214/ should mention how to add a global identifier to the config
  2. It should be easier to link to https://quick-lint-js.com/config/#_examples (I had to inspect element to get the id)
KraXen72 commented 8 months ago

isn't globals just for declaring global variables one by one, or from enviroment sets? rather, i would need quick lint to recognize all the global types declared in app.d.ts and accept them as global

strager commented 8 months ago

quick-lint-js currently does not have a mechanism to take global variables declared in one file (app.d.ts in your case) and make them visible in another file.

Some possible solutions:

  1. Rewrite your code to use import instead of globals.
  2. Manually replicate app.d.ts to your quick-lint-js.config.
  3. Use a tool which replicates app.d.ts's globals to your quick-lint-js.config. (This tool does not yet exist.)
  4. Use a "globals-files" feature in quick-lint-js.config to specify source files. You would list "globals-files": ["src/app.d.ts"]. (This feature does not yet exist.)
  5. Have quick-lint-js auto-scan for .ts and .d.ts files which declare globals, similar to what TypeScript's compiler does. (This feature does not yet exist.)

@KraXen72 Which solutions sound good to you?

KraXen72 commented 8 months ago

while 1 is a good short term solution, i think 5 would be the best - scanning, relevant .d.ts files in the project (as far as i understand it, quicklint already does scanning and parsing in these files, since i get quicklint warnings in them), and parsing declare global blocks or other ways to declare global types, so quicklint recognizes global types and supresses the unknown type warning.

until this is implemented, a stopgap solution could be adding the option to disable the 'unknown type' warning, and leave known/unknown logic to the typescript LSP. I think many people use quicklint alongside the ts lsp, and not instead of it.

while importing and exporting types is likely a better way to manage types than globals, for a lot of projects i either don't have that many types (they fit in 1 file), or i don't want to put in the extra effort of import/export management for types too. yes, autoimport extensions exist and they do help, but i found they often make mistakes when modifying existing imports, or just refuse to create 'import type {}' imports.

strager commented 8 months ago

as far as i understand it, quicklint already does scanning and parsing in these files, since i get quicklint warnings in them

quick-lint-js only reports diagnostics for files you open in your editor. quick-lint-js does not scan all files in your project automatically.

I did want to implement project-wide analysis, but only by following import statements. Your use case suggests that I need to follow more than just import statements. I'll keep this in consideration when designing the analysis.

a stopgap solution could be adding the option to disable the 'unknown type' warning, and leave known/unknown logic to the typescript LSP.

1174


In the next few months, I don't plan on implementing a proper fix for this issue. If you want to avoid the warnings but continue using quick-lint-js, you can configure quick-lint-js.

KraXen72 commented 8 months ago

for now, i'm using oxc linter, but i like quick-lint more - i'll try configuring it in a bit. thanks!