Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
eslint-import-resolver-typescript
Advanced tools
This plugin adds `TypeScript` support to `eslint-plugin-import`
The eslint-import-resolver-typescript package is a plugin for ESLint that helps in resolving import paths for TypeScript files. It allows ESLint to understand TypeScript syntax and resolve paths according to the TypeScript compiler options. This is particularly useful when using features like path aliases and custom module directories in TypeScript projects.
Resolving TypeScript Path Aliases
This feature allows the resolver to use TypeScript's path aliases defined in tsconfig.json to resolve modules, which helps ESLint to understand custom path mappings.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// use <root>/tsconfig.json
project: '.'
}
}
}
};
Support for Multiple tsconfig Files
The resolver can be configured to use multiple tsconfig.json files, which is useful in monorepos or projects with multiple TypeScript configurations.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// a list of tsconfig.json to use
project: ['tsconfig.json', 'packages/*/tsconfig.json']
}
}
}
};
Ignoring TypeScript Errors
This feature allows the resolver to ignore TypeScript errors when resolving paths. It is useful when you want ESLint to focus on import/export issues without being blocked by TypeScript compilation errors.
module.exports = {
settings: {
'import/resolver': {
typescript: {
// ignore TypeScript errors
alwaysTryTypes: true
}
}
}
};
This package provides a resolver for the ESLint plugin import that resolves node modules and commonjs modules. It does not have TypeScript-specific features but is suitable for projects using plain JavaScript.
This resolver is designed for projects using Webpack and allows ESLint to resolve module paths based on the Webpack configuration. It supports features like Webpack aliases and modules, which can be similar to TypeScript path aliases but is tailored for Webpack-specific configurations.
This package integrates with Babel and allows ESLint to resolve modules using Babel's module resolver plugin. It is similar to eslint-import-resolver-typescript in that it helps resolve non-standard module paths, but it is focused on Babel rather than TypeScript.
This plugin adds TypeScript
support to eslint-plugin-import
(Or maybe you want to try eslint-plugin-i
for faster speed)
This means you can:
import
/require
files with extension .cts
/.mts
/.ts
/.tsx
/.d.cts
/.d.mts
/.d.ts
paths
defined in tsconfig.json
@types/*
definitions over plain .js
/.jsx
imports/exports
fields support in package.json
enhanced-resolve
After version 2.0.0, .d.ts
will take higher priority then normal .js
/.jsx
files on resolving node_modules
packages in favor of @types/*
definitions or its own definition.
If you're facing some problems on rules import/default
or import/named
from eslint-plugin-import
, do not post any issue here, because they are just working exactly as expected on our sides, take import-js/eslint-plugin-import#1525 as reference or post a new issue to eslint-plugin-import
instead.
# npm
npm i -D eslint-plugin-import eslint-import-resolver-typescript
# pnpm
pnpm i -D eslint-plugin-import eslint-import-resolver-typescript
# yarn
yarn add -D eslint-plugin-import eslint-import-resolver-typescript
Important when using eslint-plugin-i
and npm
: Use npm i -D eslint-plugin-import@eslint-plugin-i@latest eslint-import-resolver-typescript
, or you will end up with both eslint-plugin-import
and eslint-plugin-i
in your node_modules.
eslint.config.js
If you are using eslint-plugin-import-x@>=4.5.0
, you can use import/require to reference eslint-import-resolver-typescript
directly in your ESLint flat config:
// eslint.config.js
const {
createTypeScriptImportResolver,
} = require('eslint-import-resolver-typescript')
module.exports = [{
settings: {
"import/resolver-next": [
createTypeScriptImportResolver({
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
// Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default
// use <root>/path/to/folder/tsconfig.json
project: "path/to/folder",
// Multiple tsconfigs (Useful for monorepos)
// use a glob pattern
project: "packages/*/tsconfig.json",
// use an array
project: [
"packages/module-a/tsconfig.json",
"packages/module-b/tsconfig.json"
],
// use an array of glob patterns
project: [
"packages/*/tsconfig.json",
"other-packages/*/tsconfig.json"
]
}),
];
}
}]
But if you are using eslint-plugin-import
or the older version of eslint-plugin-import-x
, you can't use require/import:
// eslint.config.js
module.exports = [
{
settings: {
'import/resolvers': {
typescript: {
alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
// Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default
// use <root>/path/to/folder/tsconfig.json
project: 'path/to/folder',
// Multiple tsconfigs (Useful for monorepos)
// use a glob pattern
project: 'packages/*/tsconfig.json',
// use an array
project: [
'packages/module-a/tsconfig.json',
'packages/module-b/tsconfig.json',
],
// use an array of glob patterns
project: [
'packages/*/tsconfig.json',
'other-packages/*/tsconfig.json',
],
},
},
},
},
]
.eslintrc
Add the following to your .eslintrc
config:
{
"plugins": ["import"],
"rules": {
// turn on errors for missing imports
"import/no-unresolved": "error"
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {
"alwaysTryTypes": true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
// Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default
// use <root>/path/to/folder/tsconfig.json
"project": "path/to/folder",
// Multiple tsconfigs (Useful for monorepos)
// use a glob pattern
"project": "packages/*/tsconfig.json",
// use an array
"project": [
"packages/module-a/tsconfig.json",
"packages/module-b/tsconfig.json"
],
// use an array of glob patterns
"project": [
"packages/*/tsconfig.json",
"other-packages/*/tsconfig.json"
]
}
}
}
}
enhanced-resolve
conditionNames
Default:
[
"types",
"import",
// APF: https://angular.io/guide/angular-package-format
"esm2020",
"es2020",
"es2015",
"require",
"node",
"node-addons",
"browser",
"default"
]
extensions
Default:
[
// `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
".ts",
".tsx",
".d.ts",
".js",
".jsx",
".json",
".node"
]
extensionAlias
Default:
{
".js": [
".ts",
// `.tsx` can also be compiled as `.js`
".tsx",
".d.ts",
".js"
],
".jsx": [".tsx", ".d.ts", ".jsx"],
".cjs": [".cts", ".d.cts", ".cjs"],
".mjs": [".mts", ".d.mts", ".mjs"]
}
mainFields
Default:
[
"types",
"typings",
// APF: https://angular.io/guide/angular-package-format
"fesm2020",
"fesm2015",
"esm2020",
"es2020",
"module",
"jsnext:main",
"main"
]
You can pass through other options of enhanced-resolve
directly
You can reuse defaultConditionNames
, defaultExtensions
, defaultExtensionAlias
and defaultMainFields
by require/import
them directly
yarn test
passes without a failure.yarn lint
passes without conflicts.yarn type-coverage
.We have GitHub Actions which will run the above commands on your PRs.
If either fails, we won't be able to merge your PR until it's fixed.
1stG | RxTS | UnTS |
---|---|---|
1stG | RxTS | UnTS |
---|---|---|
Detailed changes for each release are documented in CHANGELOG.md.
3.7.0
#326 93ea130
Thanks @SukkaW! - This version has implemented the eslint-plugin-import-x
's v3 resolver interface. This allows you to use import/require to reference eslint-import-resolver-typescript
directly in your ESLint flat config:
Previously
// eslint.config.js
module.exports = {
settings: {
'import-x/resolver': {
typescript: {
alwaysTryTypes: true,
},
// or
require.resolve('eslint-import-resolver-typescript'):
alwaysTryTypes: true,
}
}
}
}
Now
// eslint.config.js
const {
createTypeScriptImportResolver,
} = require('eslint-import-resolver-typescript')
module.exports = {
settings: {
'import-x/resolver-next': [
createTypeScriptImportResolver({
alwaysTryTypes: true,
}),
],
},
}
Note that this only works with eslint-plugin-import-x@>=4.5.0
. You can't use createTypeScriptImportResolver
with the older versions of eslint-plugin-import-x
or any existing versions of eslint-plugin-import
.
FAQs
This plugin adds `TypeScript` support to `eslint-plugin-import`
The npm package eslint-import-resolver-typescript receives a total of 7,858,506 weekly downloads. As such, eslint-import-resolver-typescript popularity was classified as popular.
We found that eslint-import-resolver-typescript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.