Use built-in JavaScript operators to determine the type at runtime:
function checkType(value: any): void {
if (typeof value === "string") {
console.log("Value is a string");
} else if (typeof value === "number") {
console.log("Value is a number");
}
// And so on…
}
checkType("test"); // Output: Value is a string
| Type | Check |
|---|---|
| string | typeof s === "string" |
| number | typeof n === "number" |
| boolean | typeof b === "boolean" |
| undefined | typeof u === "undefined" |
| function | typeof f === "function" |
| array | Array.isArray(a) |