value * 5; if (value === 123) { // equality // %inferred-type: 123 value; value.length; if (typeof value === 'string') { // type guard // %inferred-type: string value.test('abc'); assertionFunction(value); // %inferred-type: RegExp value; value.test
TypeScript 类型系统的一个有趣特征是,同一变量在不同位置可以具有不同的静态类型: const arr = []; // %inferred-type: any[] arr; arr.push (123); // %inferred-type: number[] arr; arr.push('abc'); // %inferred-type: (string | number)[] arr;
类型系统一个有趣的特点是,同一个变量在不同的位置可以有不同的静态类型: const arr = []; // %inferred-type: any[] arr; arr.push(123); // %inferred-type: number[] arr; arr.push('abc'); // %inferred-type: (string | number)[] arr; 4.
== null && key in dict) { 5 // %inferred-type: object 6 dict; 7 8 // @ ts-ignore:元素隐式具有“ 来检测丢失的 Map 条目(A 行): 1function getLength(strMap: Map<string, string>, key: string): number { 2 // %inferred-type const value = strMap.get(key); 4 if (value === undefined) { // (A) 5 return -1; 6 } 7 8 // %inferred-type