how check type is string in typescript

In TypeScript, you can use the typeof operator to check if a variable is a string. Here's an example:

index.ts
function isString(input: any): boolean {
    return typeof input === 'string';
}

// Example usage
console.log(isString("Hello"));   // Output: true
console.log(isString(123));       // Output: false
200 chars
8 lines

In the isString function, we use the typeof operator to check if the input is a string by comparing it with 'string'. The function returns true if the input is a string and false otherwise.

gistlibby LogSnag