taro
taro copied to clipboard
fix(cli): 更新内置默认模板 `tsconfig.json`
这个 PR 做了什么? (简要描述所做更改)
skipLibCheck
- 默认值:
false
Skip type checking of declaration files.
通常情况下我们不需要关心第三方库的 TS 语法问题, 所以非常建议开启这个选项以屏蔽一些无关紧要的错误 。
需要注意的是,这个选项只会忽略 .d.ts
文件,如果第三方库包含了 .ts
文件还是会继续检查错误的:
- https://github.com/NervJS/taro/issues/12180
- https://github.com/NervJS/taro/pull/15390
typeRoots
By default all visible ”
@types
” packages are included in your compilation. Packages innode_modules/@types
of any enclosing folder are considered visible. For example, that means packages within./node_modules/@types/
,../node_modules/@types/
,../../node_modules/@types/
, and so on.
默认情况下,所有 @types 包下( 包括上层 node_modules
)的类型定义会在编译过程中被包含进来,显式配置这个选项后就会将这个范围收缩在一个有限的范围之内。 除非有特殊需求,否则不需要配置这个选项,因为默认行为已经很好了。
配置 typeRoots
会导致需要手动添加一堆目录:
- https://github.com/microsoft/TypeScript/issues/30855
引入 sass 后会出现下面的错误:
error TS2688: Cannot find type definition file for 'sass'.
The file is in the program because:
Entry point for implicit type library 'sass'
这是因为 [email protected]
引入一个重大变更:指定了 typeRoots
选项但对任何 typeRoots
目录的解析失败时,不再向上遍历 node_modules
进行解析了。 👉 官方文档
@types/sass 最新版本是一个空包,会解析失败,就不再继续从 sass 包查找了。
typeRoots
和 types
常被滥用于屏蔽第三方库的错误,正确的姿势应该是开启 skipLibCheck
选项并补全缺失的类型定义。
noEmit
- 默认值:
false
Do not emit compiler output files like JavaScript source code, source-maps or declarations.
默认情况下 tsc 命令会将编译结果输出为文件( 例如 .js
、 .d.ts
、 .map
),这在开发 lib 项目时非常有用,但是对非 lib 项目来说是没用的,因为通常会用到 Webpack 、 Babel 之类的工具来转换源码。
建议开启这个选项,这样 tsc 命令仅用于检查 TS 语法问题。
declaration
- 默认值:
true
ifcomposite
;false
otherwise.Generate
.d.ts
files for every TypeScript or JavaScript file inside your project. These.d.ts
files are type definition files which describe the external API of your module. With.d.ts
files, tools like TypeScript can provide intellisense and accurate types for un-typed code.
生成与源码对应的 .d.ts
文件,通常只有开发 lib 项目的时候才需要开启这个选项。
已知的问题:使用 pnpm 安装依赖后,间接依赖的项目( 幽灵依赖 )没有被包含在 node_modules
中,这会导致生成 .d.ts
文件时报错。( 参考 )
error TS2742: The inferred type of 'default' cannot be named without a reference to '.pnpm/@[email protected]/node_modules/@unocss/preset-mini/dist/shared/preset-mini.P5Rzuhf5'. This is likely not portable. A type annotation is neces
sary.
IDEA 进行语法校验用到下面的命令,同样会报错:
tsc --noEmit false --declaration true
建议非 lib 项目关闭这个选项。
preserveConstEnums
- 默认值:
false
Do not erase
const enum
declarations in generated code.const enum
s provide a way to reduce the overall memory footprint of your application at runtime by emitting the enum value instead of a reference.
源码:
const enum Album {
JimmyEatWorldFutures = 1,
TubRingZooHypothesis = 2,
DogFashionDiscoAdultery = 3,
}
const selectedAlbum = Album.JimmyEatWorldFutures;
if (selectedAlbum === Album.JimmyEatWorldFutures) {
console.log("That is a great choice.");
}
"preserveConstEnums": false
的编译结果:
const selectedAlbum = 1 /* Album.JimmyEatWorldFutures */;
if (selectedAlbum === 1 /* Album.JimmyEatWorldFutures */) {
console.log("That is a great choice.");
}
"preserveConstEnums": true
的编译结果:
var Album;
(function (Album) {
Album[Album["JimmyEatWorldFutures"] = 1] = "JimmyEatWorldFutures";
Album[Album["TubRingZooHypothesis"] = 2] = "TubRingZooHypothesis";
Album[Album["DogFashionDiscoAdultery"] = 3] = "DogFashionDiscoAdultery";
})(Album || (Album = {}));
const selectedAlbum = 1 /* Album.JimmyEatWorldFutures */;
if (selectedAlbum === 1 /* Album.JimmyEatWorldFutures */) {
console.log("That is a great choice.");
}
对于使用 const enum
关键字定义的枚举类型 Album
,会在编译时将所有的枚举值 Album.xxx
替换为实际的字面量。默认会擦除生成的常量对象 Album
,如果开启了 preserveConstEnums
则不擦除。
因为 enum
加了 const
关键字后枚举类整体就不能当作一个对象来使用了,只能使用具体的枚举值。
TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
所以这个选项的意义只是为了查看源码,并不影响实际代码,没必要启用。
removeComments
- 默认值:
false
Strips all comments from TypeScript files when converting into JavaScript.
sourceMap
- 默认值:
false
Enables the generation of sourcemap files . These files allow debuggers and other tools to display the original TypeScript source code when actually working with the emitted JavaScript files. Source map files are emitted as
.js.map
( or.jsx.map
) files next to the corresponding.js
output file.
作为非 lib 项目,并不需要关心 tsc 的编译产物,所以 removeComments
、sourceMap
这些配置都是多余的。
noUnusedLocals
- 默认值:
false
Report errors on unused local variables.
noUnusedParameters
- 默认值:
false
Report errors on unused parameters in functions.
检测在代码中声明了但未使用的局部变量,这些配置是多余的,因为这是 ESLint 的工作。
其他
基础配置从 @tsconfig/recommended 进行复制。
这个 PR 是什么类型? (至少选择一个)
- [x] 错误修复(Bugfix) issue: fix #
- [ ] 新功能(Feature)
- [ ] 代码重构(Refactor)
- [ ] TypeScript 类型定义修改(Typings)
- [ ] 文档修改(Docs)
- [ ] 代码风格更新(Code style update)
- [ ] 其他,请描述(Other, please describe):
这个 PR 涉及以下平台:
- [x] 所有小程序
- [ ] 微信小程序
- [ ] 支付宝小程序
- [ ] 百度小程序
- [ ] 字节跳动小程序
- [ ] QQ 轻应用
- [ ] 京东小程序
- [ ] 快应用平台(QuickApp)
- [ ] Web 平台(H5)
- [ ] 移动端(React-Native)
- [ ] 鸿蒙(harmony)
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 58.51%. Comparing base (
ba27009
) to head (86f4020
). Report is 107 commits behind head on main.
:exclamation: Current head 86f4020 differs from pull request most recent head b1272f7. Consider uploading reports for the commit b1272f7 to get more accurate results
Additional details and impacted files
@@ Coverage Diff @@
## main #15417 +/- ##
=======================================
Coverage 58.51% 58.51%
=======================================
Files 491 491
Lines 21891 21891
Branches 5624 5624
=======================================
Hits 12810 12810
+ Misses 7916 7897 -19
- Partials 1165 1184 +19
Flag | Coverage Δ | |
---|---|---|
taro-cli | 63.29% <ø> (ø) |
|
taro-runner | 46.25% <ø> (ø) |
|
taro-runtime | 65.35% <ø> (ø) |
|
taro-web | 40.28% <ø> (ø) |
Flags with carried forward coverage won't be shown. Click here to find out more.
辛苦把模板库的也一起改了吧😁 https://github.com/NervJS/taro-project-templates/tree/v3.6-rs https://github.com/NervJS/taro-project-templates/tree/v4.0
辛苦把模板库的也一起改了吧😁 https://github.com/NervJS/taro-project-templates/tree/v3.6-rs https://github.com/NervJS/taro-project-templates/tree/v4.0
👌 不过模板有点多,测试需要点时间
辛苦把模板库的也一起改了吧😁 https://github.com/NervJS/taro-project-templates/tree/v3.6-rs https://github.com/NervJS/taro-project-templates/tree/v4.0
done
- https://github.com/NervJS/taro-project-templates/pull/128
- https://github.com/NervJS/taro-project-templates/pull/129
"strict": true 也可以加上, 还可以看看能不能升级到 typescript5.4.4 使用 "module": "preserve" https://www.totaltypescript.com/tsconfig-cheat-sheet https://devblogs.microsoft.com/typescript/announcing-typescript-5-4/#support-for-require-calls-in---moduleresolution-bundler-and---module-preserve
"strict": true 也可以加上
更推荐使用 extends
@tsconfig/recommended ,可以精简代码:
"extends": "@tsconfig/recommended/tsconfig.json"
但考虑到很多人不熟悉这个用法,到处乱复制配置反而错误覆盖了,干脆直接复制过来,用户按需修改:
// https://npmjs.com/package/@tsconfig/recommended
"target": "es2017",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
还可以看看能不能升级到 typescript5.4.4 使用 "module": "preserve" https://www.totaltypescript.com/tsconfig-cheat-sheet https://devblogs.microsoft.com/typescript/announcing-typescript-5-4/#support-for-require-calls-in---moduleresolution-bundler-and---module-preserve
测试了下, "module": "preserve"
的作用似乎是在 "module": "esnext"
的基础上支持 import bar = require("some-package/bar")
语法( "module": "commonjs"
支持这个语法 ),其他情况下和 "module": "esnext"
表现一致:
- esm.ts 编译为 esm.js
- cjs.ts 编译为 cjs.js
非 lib 项目应该是用不上这个 import ... = require(...)
语法的。
再加上设置了 "noEmit": true
,选择 "module": "esnext"
还是 "module": "commonjs"
都一样