You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/rules/no-restricted-imports.md
+168Lines changed: 168 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -282,6 +282,61 @@ import { AllowedObject } from "foo";
282
282
283
283
:::
284
284
285
+
#### allowTypeImports (TypeScript only)
286
+
287
+
Whether to allow [Type-Only Imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) for a path. This includes type-only `export` statements, as they are equivalent to re-exporting an `import`. Default: `false`.
288
+
289
+
Examples of **incorrect** code for `allowTypeImports` in `paths`:
message: "Please use 'Baz' from 'import-foo' as a type only."
333
+
}]}]*/
334
+
335
+
import { Bar, typeBaz } from"import-foo";
336
+
```
337
+
338
+
:::
339
+
285
340
### patterns
286
341
287
342
This is also an object option whose value is an array. This option allows you to specify multiple modules to restrict using `gitignore`-style patterns or regular expressions.
@@ -773,6 +828,119 @@ import { isEmpty } from 'utils/collection-utils';
773
828
774
829
:::
775
830
831
+
#### allowTypeImports (TypeScript only)
832
+
833
+
Whether to allow [Type-Only Imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) for a path. This includes type-only `export` statements, as they are equivalent to re-exporting an `import`. Default: `false`.
834
+
835
+
```json
836
+
"no-restricted-imports": ["error", {
837
+
"patterns": [{
838
+
"group": ["import/private/*"],
839
+
"allowTypeImports": true,
840
+
}]
841
+
}]
842
+
```
843
+
844
+
Examples of **incorrect** code for `allowTypeImports` in `patterns`:
message: "Please use 'Baz' from 'import/private/*' as a type only."
888
+
}]}]*/
889
+
890
+
import { Bar, typeBaz } from"import/private/bar";
891
+
```
892
+
893
+
:::
894
+
895
+
## Known Limitations
896
+
897
+
TypeScript [`import = require()` syntax](https://www.typescriptlang.org/docs/handbook/2/modules.html#es-module-syntax-with-commonjs-behavior) is valid and the rule can recognize and lint such instances, but with certain limitations.
898
+
899
+
You can only fully restrict these imports, you cannot restrict them based on specific import names like `importNames`, `allowImportNames`, `importNamePattern`, or `allowImportNamePattern` options.
900
+
901
+
Examples of **incorrect** code for TypeScript import equals declarations:
**Note:** Import name restrictions do not apply to TypeScript import equals declarations. The following configuration will not restrict the import equals declaration:
926
+
927
+
::: correct { "sourceType": "module" }
928
+
929
+
```ts
930
+
/*eslint no-restricted-imports: ["error", {
931
+
"paths": [{
932
+
"name": "foo",
933
+
"importNames": ["foo"]
934
+
}]
935
+
}]*/
936
+
937
+
// This import equals declaration will NOT be restricted
938
+
// even though it imports the entire module
939
+
importfoo=require("foo");
940
+
```
941
+
942
+
:::
943
+
776
944
## When Not To Use It
777
945
778
946
Don't use this rule or don't include a module in the list for this rule if you want to be able to import a module in your project without an ESLint error or warning.
0 commit comments