要求当一个对象在字符串化时提供了有用的信息,才能调用“toString()”方法。
// These types all have useful .toString()s
const num = 123;
export const v1 = 'Text' + true;
export const v2 = `Value: ${num}`;
(() => {
console.info('arrow function');
}).toString(); interface MyType {
name: string;
}
// Passing an object or class instance to string concatenation:
const obj: MyType = {
name: 'object'
};
export const v1 = '' + obj;
class MyClass {}
const value = new MyClass();
export const v2 = value + '';
// Interpolation and manual .toString() calls too:
export const v3 = `Value: ${value}`;
export const v4 = obj.toString();