function isString(x: unknown): x is string {
return typeof x === 'string'
}
isString('hello') // β true
isString(42) // β falseA type guard with a type predicate (x is string) narrows the type in the calling scope.
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