declares globals and exports in .d.ts file not recognized
KraXen72 opened this issue · 6 comments
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:
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.
I think the documentation needs to be improved.
- https://quick-lint-js.com/errors/E0214/ should mention how to add a global identifier to the config
- It should be easier to link to https://quick-lint-js.com/config/#_examples (I had to inspect element to get the id)
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
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:
- Rewrite your code to use
import
instead of globals. - Manually replicate app.d.ts to your quick-lint-js.config.
- Use a tool which replicates app.d.ts's globals to your quick-lint-js.config. (This tool does not yet exist.)
- 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.) - 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?
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.
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.
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.
for now, i'm using oxc linter, but i like quick-lint more - i'll try configuring it in a bit. thanks!