分类 TypeScript 下的文章

  1. toLocaleString(),当数字是四位数及以上时,从右往左数,每三位用分号隔开,并且小数点后只保留三位;而toString()单纯将数字转换为字符串。
  2. toLocaleString(),当目标是标准时间格式时,输出简洁年月日,时分秒;而toString()输出国际表述字符串。
var num = new Number(1777.123488); 
console.log(num.toLocaleString());  // 输出:1,777.123
console.log(num.toString());  // 输出:1777.123488

var dateStr = new Date();
console.log(dateStr.toLocaleString());  // 输出:2022/2/15 16:48:35
console.log(dateStr.toString());  // 输出:Tue Feb 15 2022 16:48:58 GMT+0800 (中国标准时间)

元组类型用来表示已知元素数量和类型的数组,各元素的类型不必相同,对应位置的类型需要相同。

let x: [a:string, b:number];
x = ['abc', 100] //okay
x = [12, 'hello']; //error
console.log(x[0]); //abc

当接口继承了一个类类型时,它会继承类的成员但不包括其实现。 就好像接口声明了所有类中存在的成员,但并没有提供具体实现一样。 接口同样会继承到类的private和protected成员。 这意味着当你创建了一个接口继承了一个拥有私有或受保护的成员的类时,这个接口类型只能被这个类或其子类所实现(implement)。

元组类型:

表示一个已知元素数量和类型的数组,各元素的类型不必相同。 比如,你可以定义一对值分别为stringnumber类型的元组。


// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK
// Initialize it incorrectly
x = [10, 'hello']; // Error

console.log(x[0].substr(1)); // OK
console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'

x[3] = 'world'; // OK, 字符串可以赋值给(string | number)类型

console.log(x[5].toString()); // OK, 'string' 和 'number' 都有 toString
x[6] = true; // Error, 布尔不是(string | number)类型