useImportExtensions
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/correctness/useImportExtensions¥Diagnostic Category:
lint/correctness/useImportExtensions -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
此规则属于以下域:
¥This rule belongs to the following domains:
¥How to configure
{ "linter": { "rules": { "correctness": { "useImportExtensions": "error" } } }}¥Description
强制显式比较值的 、、 或 属性。
¥Enforce file extensions for relative imports.
浏览器、Deno 和 Node.js 本身不支持从 JavaScript 模块导入没有扩展名的文件。此规则强制要求相对导入必须使用文件扩展名,以使代码更加一致且正确。
¥Browsers, Deno, and Node.js do not natively support importing files without extensions from JavaScript modules. This rule enforces the use of file extensions for relative imports to make the code more consistent — and correct.
在某些情况下,工具也可以从显式的文件扩展名中受益,因为它们无需猜测要解析哪个文件。
¥In some cases, tooling can also benefit from explicit file extensions, because they do not need to guess which file to resolve.
该规则会检查静态导入 (import ... from "...") 以及动态导入,例如 import(...) 和 require(...)。
¥The rule checks both static imports (import ... from "...") as well as
dynamic imports such as import(...) and require(...).
¥Examples
¥Invalid
以下示例假设这些导入将解析为具有 .txt 扩展名的文件。完全无法解析的导入不会触发诊断信息。
¥The following examples assume these imports will resolve to a file with an extension. Imports that don’t resolve at all will not trigger a diagnostic.
import "./foo";import "./foo/";import "../";import "../.";import("./foo");require("./foo");¥Valid
import "biome";import "./foo.js";import "./bar/index.js";import("./foo.js");require("./foo.js");¥Options
该规则提供了如下所述的选项。
¥The rule provides the options described below.
forceJsExtensions
Section titled “forceJsExtensions”通常,此规则建议使用项目中找到的模块的扩展。例如,TypeScript 文件可以使用 .ts 或 .tsx。如果此选项设置为 true,则无论项目中的文件扩展名如何,该规则始终建议使用 .js。
¥Normally, this rule suggests to use the extension of the module that is
found in your project. For instance, .ts or .tsx for a TypeScript
file. If this option is set to true, the rule will always suggest to
use .js regardless of the extension in your project.
如果你在使用 tsc 构建代码时使用了 "module": "node16" 设置,这将非常有用。
¥This is useful if you use the "module": "node16" setting when building
your code with tsc.
默认:false
¥Default: false
{ "linter": { "rules": { "correctness": { "useImportExtensions": { "options": { "forceJsExtensions": true } } } } }}¥Editor Configuration
如果你使用 Visual Studio Code,可以通过在 settings 中配置 javascript.preferences.importModuleSpecifierEnding 和 typescript.preferences.importModuleSpecifierEnding,确保在自动导入变量时添加文件扩展名。
¥If you use Visual Studio Code, you can ensure that it adds the file
extension when automatically importing a variable by configuring
javascript.preferences.importModuleSpecifierEnding and
typescript.preferences.importModuleSpecifierEnding
in your settings.
¥Caveats
如果你使用的是 TypeScript,则需要 TypeScript 5.0 或更高版本,并且请确保在 tsconfig.json 中设置 allowImportingTsExtensions: true。
¥If you are using TypeScript, TypeScript version 5.0 or later is
required, also make sure to set
allowImportingTsExtensions: true
in your tsconfig.json.
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号