Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): 更新内置默认模板 tsconfig.json #15417

Closed
wants to merge 3 commits into from

Conversation

anyesu
Copy link
Contributor

@anyesu anyesu commented Mar 29, 2024

这个 PR 做了什么? (简要描述所做更改)

skipLibCheck

  • 默认值: false

Skip type checking of declaration files.

通常情况下我们不需要关心第三方库的 TS 语法问题, 所以非常建议开启这个选项以屏蔽一些无关紧要的错误

需要注意的是,这个选项只会忽略 .d.ts 文件,如果第三方库包含了 .ts 文件还是会继续检查错误的:

typeRoots

By default all visible@types ” packages are included in your compilation. Packages in node_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 会导致需要手动添加一堆目录:

引入 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 包查找了。

typeRootstypes 常被滥用于屏蔽第三方库的错误,正确的姿势应该是开启 skipLibCheck 选项并补全缺失的类型定义。

noEmit

  • 默认值: false

Do not emit compiler output files like JavaScript source code, source-maps or declarations.

默认情况下 tsc 命令会将编译结果输出为文件( 例如 .js.d.ts.map ),这在开发 lib 项目时非常有用,但是对非 lib 项目来说是没用的,因为通常会用到 WebpackBabel 之类的工具来转换源码。

建议开启这个选项,这样 tsc 命令仅用于检查 TS 语法问题。

declaration

  • 默认值: true if composite ; 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

image

建议非 lib 项目关闭这个选项。

preserveConstEnums

  • 默认值: false

Do not erase const enum declarations in generated code. const enums 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 的编译产物,所以 removeCommentssourceMap 这些配置都是多余的。

noUnusedLocals

  • 默认值: false

Report errors on unused local variables.

noUnusedParameters

  • 默认值: false

Report errors on unused parameters in functions.

检测在代码中声明了但未使用的局部变量,这些配置是多余的,因为这是 ESLint 的工作。


其他

基础配置从 @tsconfig/recommended 进行复制。


这个 PR 是什么类型? (至少选择一个)

  • 错误修复(Bugfix) issue: fix #
  • 新功能(Feature)
  • 代码重构(Refactor)
  • TypeScript 类型定义修改(Typings)
  • 文档修改(Docs)
  • 代码风格更新(Code style update)
  • 其他,请描述(Other, please describe):

这个 PR 涉及以下平台:

  • 所有小程序
  • 微信小程序
  • 支付宝小程序
  • 百度小程序
  • 字节跳动小程序
  • QQ 轻应用
  • 京东小程序
  • 快应用平台(QuickApp)
  • Web 平台(H5)
  • 移动端(React-Native)
  • 鸿蒙(harmony)

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 58.51%. Comparing base (ba27009) to head (86f4020).
Report is 107 commits behind head on main.

❗ 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

Impacted file tree graph

@@           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.

see 11 files with indirect coverage changes

@luckyadam
Copy link
Member

@anyesu
Copy link
Contributor Author

anyesu commented Apr 1, 2024

辛苦把模板库的也一起改了吧😁 https://github.com/NervJS/taro-project-templates/tree/v3.6-rs https://github.com/NervJS/taro-project-templates/tree/v4.0

👌 不过模板有点多,测试需要点时间

This was referenced Apr 6, 2024
This was referenced Jul 6, 2024
@liqinuo liqinuo changed the base branch from main to 3.x July 16, 2024 06:54
@ZEJIA-LIU ZEJIA-LIU deleted the branch NervJS:3.x July 16, 2024 07:16
@ZEJIA-LIU ZEJIA-LIU closed this Jul 16, 2024
@ZEJIA-LIU ZEJIA-LIU reopened this Jul 16, 2024
@anyesu anyesu closed this Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typings Area - TypeScript Typings 相关
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants