eslint-import-resolver-typescript
eslint-import-resolver-typescript copied to clipboard
`import/extensions`, `import/no-unresolved` don't highlight import of nonexistent .js
(Original issue is posted at https://github.com/import-js/eslint-plugin-import/issues/3015.)
Take this simple setup:
eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import * as pluginImport from 'eslint-plugin-import';
export default [
eslint.configs.recommended,
...tseslint.configs.recommended,
{
files: ['**/*.ts'],
plugins: {
'import': pluginImport,
},
settings: {
'import/resolver': {
node: true,
typescript: true,
},
},
rules: {
'import/extensions': 'error',
'import/no-unresolved': 'error',
},
},
];
index.ts
import { num } from './exported.js';
exported.ts
export const num = 3;
In index.ts, ./exported.js
points to a nonexistent file. Neither import/extensions
, nor import/no-unresolved
highlight it though.
I narrowed down the problem to typescript: true
– as soon as I comment that line, I see both errors:
Unexpected use of file extension "js" for "./exported.js" eslint(import/extensions) Unable to resolve path to module './exported.js'. eslint(import/no-unresolved)
I assume this is the problem with eslint-import-resolver-typescript
. I tried messing with it in some ways, but to no avail.
I should also note that the highlight will appear also if:
- I change the base name to one that has no corresponding .ts filename:
Unexpected use of file extension "js" for "./exported_nonexistentBase.js" eslint(import/extensions) Unable to resolve path to module './exported_nonexistentBase.js'. eslint(import/no-unresolved)
- I change the extension to any other:
Unable to resolve path to module './exported.jsx'. eslint(import/no-unresolved)
- I rename exported.ts to exported.js so that the import exists
Unexpected use of file extension "js" for "./exported.js". eslint(import/extensions)
So, this seems to be a pretty unique case with a nonexistent .js silently mapping to an existent .ts with the same basename.