JS Built-ins
TypeScriptbeginner

Type Guard β€” isString

πŸ“– Usage Example
function isString(x: unknown): x is string {
  return typeof x === 'string'
}
isString('hello')   // β†’ true
isString(42)        // β†’ false

A type guard with a type predicate (x is string) narrows the type in the calling scope.

Type Guards: isString

A type guard is a function that returns a type predicate (e.g. x is string).
At runtime it's just a boolean, but TypeScript uses it to narrow the type in the calling scope.

Challenge: Implement isString(x: unknown): x is string.

isString('hello')  // β†’ true
isString(42)       // β†’ false
πŸ’‘ Hints (1)
  • β€’ typeof x === "string" is the standard runtime check.