mcpField
T
three-versions-types

three-versions-types

github.com/marius-townhouse开发643 次安装38 周前更新
代码仓库

关于 three-versions-types

这项 TypeScript 技能讲解运行时包、其 @types 类型声明与 TypeScript 编译器版本之间的关系。它面向发布类型声明或排查类型兼容问题的开发者,帮助分析运行时正常但类型报错的情况。

主要功能

  1. 01讲解相互关联的三个版本
  2. 02版本冲突的排查指引
  3. 03检查已安装版本的命令
  4. 04协调更新的建议

适用场景

  1. 01排查运行正常但类型报错
  2. 02管理 @types 包依赖
  3. 03发布 TypeScript 类型声明

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill three-versions-types

安装后在 Claude Code 中输入/three-versions-types确认已加载。

详细介绍

理解类型声明涉及的三个版本

概述

使用 TypeScript 类型时,三个版本必须保持一致:你使用的包版本、其类型声明(@types)版本,以及 TypeScript 本身的版本。这些版本不一致时,即使代码在运行时可以正常工作,也可能导致令人困惑的类型错误。

了解这三个版本有助于诊断和预防类型兼容性问题。

三个版本

  1. 包版本 - 运行时库(例如,[email protected])
  2. @types 版本 - 类型声明(例如,@types/[email protected])
  3. TypeScript 版本 - 你的 TypeScript 编译器(例如,[email protected])

何时使用此技能

  • 发布类型声明
  • 调试版本冲突
  • 使用 @types 包
  • 管理库依赖
  • 类型错误与运行时行为不一致

铁律

确保包、@types 和 TypeScript 版本兼容。一起更新它们,以避免莫名其妙的类型错误。

常见版本问题

// Package updated but @types not updated
import { newFeature } from 'library';  // Runtime works
// Error: Module 'library' has no exported member 'newFeature'

// TypeScript too old for new type features
// Error: Type instantiation is excessively deep
// (Newer @types use features old TS doesn't support)

// @types too new for package version
// Types reference features not in actual package

最佳实践

// package.json
{
  "dependencies": {
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@types/lodash": "^4.14.191",
    "typescript": "^5.2.2"
  }
}

检查版本

# Check all three versions
npm ls lodash @types/lodash typescript

# Check for outdated packages
npm outdated

# Update together
npm update lodash @types/lodash typescript

参考资料

  • Dan Vanderkam 著《Effective TypeScript》第 2 版
  • 条目 66:理解类型声明涉及的三个版本

评论

支持 Markdown