String Formatting in TypeScript

interface Point {
    x: number;
    y: number;
}

function main() {
    // TypeScript offers several ways to format strings and values.
    // Here are some examples of common string formatting tasks.

    // For basic string interpolation, we use template literals.
    const p: Point = { x: 1, y: 2 };
    console.log(`struct1: ${p}`);

    // To include object properties, we can use the spread operator.
    console.log(`struct2: ${JSON.stringify({ ...p })}`);

    // For a more detailed representation, we can use JSON.stringify.
    console.log(`struct3: ${JSON.stringify(p)}`);

    // To print the type of a value, we can use the typeof operator.
    console.log(`type: ${typeof p}`);

    // Formatting booleans is straightforward.
    console.log(`bool: ${true}`);

    // For integers, we can use template literals or toString() method.
    console.log(`int: ${123}`);

    // To print a binary representation, we use toString(2).
    console.log(`bin: ${(14).toString(2)}`);

    // To print the character corresponding to a given integer, we use String.fromCharCode().
    console.log(`char: ${String.fromCharCode(33)}`);

    // For hexadecimal representation, we use toString(16).
    console.log(`hex: ${(456).toString(16)}`);

    // For basic decimal formatting of floats, we can use toFixed().
    console.log(`float1: ${(78.9).toFixed(6)}`);

    // For scientific notation, we use toExponential().
    console.log(`float2: ${(123400000.0).toExponential()}`);
    console.log(`float3: ${(123400000.0).toExponential().toUpperCase()}`);

    // For basic string printing, we use template literals.
    console.log(`str1: ${"\"string\""}`);

    // To include quotes, we can use escape characters.
    console.log(`str2: "${"\"string\""}""`);

    // To print a hexadecimal representation of a string, we can use a helper function.
    const strToHex = (str: string) => str.split('').map(char => char.charCodeAt(0).toString(16)).join('');
    console.log(`str3: ${strToHex("hex this")}`);

    // To print a representation of a pointer, we can use the object's identity.
    console.log(`pointer: ${Object.is(p, p)}`);

    // For formatting with specific width, we can use padStart() and padEnd().
    console.log(`width1: |${(12).toString().padStart(6)}|${(345).toString().padStart(6)}|`);

    // For floats with specific width and precision, we can combine toFixed() and padStart().
    console.log(`width2: |${(1.2).toFixed(2).padStart(6)}|${(3.45).toFixed(2).padStart(6)}|`);

    // For left-justified formatting, we use padEnd().
    console.log(`width3: |${(1.2).toFixed(2).padEnd(6)}|${(3.45).toFixed(2).padEnd(6)}|`);

    // For string formatting with specific width, we use padStart() and padEnd().
    console.log(`width4: |${"foo".padStart(6)}|${"b".padStart(6)}|`);
    console.log(`width5: |${"foo".padEnd(6)}|${"b".padEnd(6)}|`);

    // In TypeScript, we typically use template literals for string formatting.
    const s = `sprintf: a ${"string"}`;
    console.log(s);

    // To print to stderr, we can use console.error.
    console.error(`io: an ${"error"}`);
}

main();

To run this TypeScript code, you would typically use a TypeScript compiler (tsc) to transpile it to JavaScript, and then run it with Node.js. Alternatively, you could use ts-node to run TypeScript directly.

$ ts-node string-formatting.ts

This will output the formatted strings to the console, demonstrating various string formatting techniques in TypeScript.

TypeScript doesn’t have direct equivalents for all of Go’s formatting verbs, but it provides other ways to achieve similar results, often using built-in methods of JavaScript objects or custom helper functions.

Remember that TypeScript is a superset of JavaScript, so all JavaScript string manipulation methods are available in TypeScript as well.