TypeScript 4.2·续1

TS_Compiler_Directory/4.2.2/lib/lib.es5.d.ts  Library referenced via 'es5' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2015.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2015.d.ts  Library referenced via 'es2015' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2016.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2016.d.ts  Library referenced via 'es2016' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2017.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2017.d.ts  Library referenced via 'es2017' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2018.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2018.d.ts  Library referenced via 'es2018' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2019.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2019.d.ts  Library referenced via 'es2019' from file 'TS_Compiler_Directory/4.2.2/lib/lib.es2020.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.es2020.d.ts  Library referenced via 'es2020' from file 'TS_Compiler_Directory/4.2.2/lib/lib.esnext.d.ts'TS_Compiler_Directory/4.2.2/lib/lib.esnext.d.ts  Library 'lib.esnext.d.ts' specified in compilerOptions... More Library References...foo.ts  Matched by include pattern '**/*' in 'tsconfig.json'

目前,TypeScript 不保证输出文件的格式 - 它在将来可能会改变。 关于这一点,我们也打算改进输出文件格式,请给出你的建议!

更多详情,请参考 PR!

改进逻辑表达式中的未被调用函数检查

感谢 Alex Tarasyuk 提供的持续改进,TypeScript 中的未调用函数检查现在也作用于 && 和 || 表达式。

在 --strictNullChecks 模式下,下面的代码会产生错误。

function shouldDisplayElement(element: Element) {    // ...    return true;}function getVisibleItems(elements: Element[]) {    return elements.filter((e) => shouldDisplayElement && e.children.length);    //                          ~~~~~~~~~~~~~~~~~~~~    // 该条件表达式永远返回 true,因为函数永远是定义了的。    // 你是否想要调用它?}

更多详情,请参考 PR。

解构出来的变量可以被明确地标记为未使用的

感谢 Alex Tarasyuk 提供的另一个 PR,你可以使用下划线(_ 字符)将解构变量标记为未使用的。

let [_first, second] = getValues();

在之前,如果 _first 未被使用,那么在启用了 noUnusedLocals 时 TypeScript 会产生一个错误。 现在,TypeScript 会识别出使用了下划线的 _first 变量是有意的未使用的变量。

更多详情,请参考 PR。

放宽了在可选属性和字符串索引签名间的限制

字符串索引签名可用于为类似于字典的对象添加类型,它表示允许使用任意的键来访问对象:

const movieWatchCount: { [key: string]: number } = {};function watchMovie(title: string) {    movieWatchCount[title] = (movieWatchCount[title] ?? 0) + 1;}

当然了,对于不在字典中的电影名而言 movieWatchCount[title] 的值为 undefined。(TypeScript 4.1 增加了 --noUncheckedIndexedAccess 选项,在访问索引签名时会增加 undefined 值。) 即便一定会有 movieWatchCount 中不存在的属性,但在之前的版本中,由于 undefined 值的存在,TypeScript 会将可选对象属性视为不可以赋值给兼容的索引签名。

type WesAndersonWatchCount = {    'Fantastic Mr. Fox'?: number;    'The Royal Tenenbaums'?: number;    'Moonrise Kingdom'?: number;    'The Grand Budapest Hotel'?: number;};declare const wesAndersonWatchCount: WesAndersonWatchCount;const movieWatchCount: { [key: string]: number } = wesAndersonWatchCount;//    ~~~~~~~~~~~~~~~ 错误!// 类型 'WesAndersonWatchCount' 不允许赋值给类型 '{ [key: string]: number; }'。//    属性 '"Fantastic Mr. Fox"' 与索引签名不兼容。//      类型 'number | undefined' 不允许赋值给类型 'number'。//        类型 'undefined' 不允许赋值给类型 'number'。 (2322)

TypeScript 4.2 允许这样赋值。 但是不允许使用带有 undefined 类型的非可选属性进行赋值,也不允许将 undefined 值直接赋值给某个属性:

type BatmanWatchCount = {    'Batman Begins': number | undefined;    'The Dark Knight': number | undefined;    'The Dark Knight Rises': number | undefined;};declare const batmanWatchCount: BatmanWatchCount;// 在 TypeScript 4.2 中仍是错误。const movieWatchCount: { [key: string]: number } = batmanWatchCount;// 在 TypeScript 4.2 中仍是错误。// 索引签名不允许显式地赋值 `undefined`。movieWatchCount["It's the Great Pumpkin, Charlie Brown"] = undefined;

这条新规则不适用于数字索引签名,因为它们被当成是类数组的并且是稠密的:

declare let sortOfArrayish: { [key: number]: string };declare let numberKeys: { 42?: string };sortOfArrayish = numberKeys;

更多详情,请参考 PR。

声明缺失的函数

感谢 Alexander Tarasyuk 提交的 PR,TypeScript 支持了一个新的快速修复功能,那就是根据调用方来生成新的函数和方法声明!