跳到正文
Skillpedia
通用编程75k · #3067

type-only-imports

它能做什么

这项技能检查 TypeScript 的导入语句,并正确标记只用于类型的导入。适用于希望打包体积更小、依赖问题更少的开发者。

适用场景

  • ✓当 TypeScript 文件导入的模块只用作类型的时候。
  • ✓当打包结果中包含似乎只为类型而引入的模块的时候。
  • ✓当你在搭建新的 TypeScript 项目,并希望有清晰的导入规则的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-only-imports/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-only-imports/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 TypeScript 构建引入了一个很大的模块,但我只把它用作类型。请帮我修复这些导入。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出只用作类型的导入,并把它们改为 import type。之后编译器会在构建代码时将其移除。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Use import type for type-only imports

Regular imports cause the JavaScript runtime to evaluate and execute the imported module even when only a TypeScript type from that module is used. This can increase bundle size, introduce unintended side effects, and create circular dependency issues that are otherwise avoidable. Using import type makes the erased-at-runtime nature of type imports explicit and enables bundlers and the TypeScript compiler to handle them optimally.

Quick Reference

  • import type is erased entirely at compile time — no runtime module evaluation
  • Prevents accidentally importing a value when only a type is needed
  • Reduces circular dependency issues caused by type-only relationships
  • verbatimModuleSyntax in TypeScript 5 enforces this automatically

Check

Review the import statements in this TypeScript file and identify any imports where only types or interfaces are used. These should use import type instead of a regular import.

Fix

Convert the identified regular imports to import type where the imported binding is only used as a TypeScript type annotation and not as a runtime value.

Explain

Explain the difference between import and import type in TypeScript, why type-only imports should be declared explicitly, and how verbatimModuleSyntax enforces this at the compiler level.

Code Review

Inspect all import statements in this file. For each import, check whether the imported name appears only in type positions (annotations, generics, implements clauses) versus runtime positions (function calls, new, typeof). Flag any regular import that is used only as a type.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/type-only-imports

常见问题

import type 有什么作用?

它告诉编译器,该导入会在构建时被删除,因此不会加载任何运行时模块。

verbatimModuleSyntax 是什么?

根据这项技能,它是 TypeScript 5 的一项设置,会自动强制执行这一规则。

这能帮助解决循环依赖吗?

这项技能指出,使用只用于类型的导入,可以减少可以避免的循环依赖。

详情

作者的原始描述

Use when auditing import statements in TypeScript files, setting up a new TypeScript project, or investigating unexpected bundle inclusions from type-only dependencies.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3067
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能