Symbol.toStringTag

When an object is converted to a string, it displays in a standard way as either '[object Object]', or '[object Array]' if it is an array. The [Symbol.toStringTag] computed property customizes the string shown after object, so that the object converted to string displays as '[object <customized string>]'. For instance, it is possible to customize the Person class so that it displays as '[object Person]':

    export class Person {        ...        get [Symbol.toStringTag]() {         return "Person";        }        ...    }    let anObject: any = new Person("Francesco", "Abbruzzese");    // shows [object Person]    alert(anObject);

Get Hands-On TypeScript for C# and .NET Core Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.