TypeScriptType Guardstype predicate
as const

type predicateType Guards

Object.keys

A user-defined type guard function that narrows the type of its argument.

Syntax

function isT(v: unknown): v is T { ... }

Example

Enter values below to update the example in real time.

function
isString
unknown
string
return
typeof
const
val
if
console
toUpperCase
HELLO
function isString(v: unknown): v is string {
  return typeof v === "string";
}
const val: unknown = "hello";
if (isString(val)) {
  console.log(val.toUpperCase()); // HELLO
}