Skip to main content
Version: Next

使用 TypeScript

pnpm 在大多数情况下应该可以很好地与 TypeScript 配合使用。

¥pnpm should work well with TypeScript out of the box most of the time.

¥Do not preserve symlinks

不应将 preserveSymlinks 设置为 true 时使用 TypeScript。TypeScript 将无法正确解析链接的 node_modules 中的类型依赖。如果你出于某种原因确实需要保留符号链接,则应将 pnpm 的 nodeLinker 设置设置为 hoisted

¥You should not use TypeScript with preserveSymlinks set to true. TypeScript will not be able to resolve the type dependencies correctly in the linked node_modules. If you do need to preserve symlinks for some reason, then you should set pnpm's nodeLinker setting to hoisted.

工作区使用情况

¥Workspace usage

如果工作区中存在不同版本的 @types/ 依赖,有时可能会遇到问题。当包需要这些类型,但依赖中没有类型依赖时,就会发生这些问题。例如,如果你的依赖中包含 antd,而它依赖于 @types/react,那么当你的工作区中有多个版本的 @types/react 时,你可能会收到编译错误。这实际上是 antd 的问题,因为它应该将 @types/react 添加到 peerDependencies。幸运的是,你可以通过扩展 antd 并添加缺少的对等依赖来解决这个问题。你可以通过将以下内容添加到你的 pnpm-workspace.yaml 来实现:

¥You might sometimes have issues if you have different versions of a @types/ dependency in a workspace. These issues happen when a package requires these types without having the type dependency in dependencies. For instance, if you have antd in your dependencies, which relies on @types/react, you might get a compilation error if there are multiple versions of @types/react in your workspace. This is actually an issue on antd's end because it should've added @types/react to peerDependencies. Luckily, you can fix this by extending antd with the missing peer dependency. You can do this either by adding this to your pnpm-workspace.yaml:

packageExtensions:
antd:
peerDependencies:
'@types/react': '*'

或者,你可以安装我们为解决这些问题 @pnpm/types-fixer 而创建的配置依赖。运行:

¥Alternatively, you can install a config dependency that we created to deal with these issues @pnpm/types-fixer. Run:

pnpm add @pnpm/types-fixer --config
pnpm config set pnpmfile node_modules/.pnpm-config/@pnpm/types-fixer/pnpmfile.cjs --location=project