For :

abc:number|string;

Use the JavaScript operator typeof:

if (typeof abc === "number") {
    // do something
}

TypeScript understands typeof 🌹

This is called a typeguard.

More

For classes you would use instanceof e.g.

class Foo {}
class Bar {} 

// Later
if (fooOrBar instanceof Foo){
  // TypeScript now knows that `fooOrBar` is `Foo`
}

There are also other type guards e.g. in etc https://basarat.gitbook.io/typescript/type-system/typeguard

Answer from basarat on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › typeof-types.html
TypeScript: Documentation - Typeof Type Operator
TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable or property:
Discussions

Getting type of a property of a TypeScript class using keyof operator - Stack Overflow
You would get a return value of the type "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function", because you're using the JavaScript typeof operator at runtime, which returns a string like "object", not the compile-time type seen by TypeScript. More on stackoverflow.com
🌐 stackoverflow.com
Getting the exact type of an object
🤔 have you tried a const assertion? const a:MyType = "a" as const More on reddit.com
🌐 r/typescript
5
0
June 8, 2022
How to get type from array of objects
type MyType = typeof myarr[number]["field1"]; More on reddit.com
🌐 r/typescript
9
16
August 19, 2022
Create a type based on value in object
This should work for you: type ArticleDisplay = { [T in keyof Article]: { key: T, label: Capitalize, transform: (value: Article[T]) => string | number } }[keyof Article]; Produces the following type: type ArticleDisplay = { key: "title"; label: "Title"; transform: (value: string) => string | number; } | { key: "published"; label: "Published"; transform: (value: Date) => string | number; } | { key: "views"; label: "Views"; transform: (value: number) => string | number; } More on reddit.com
🌐 r/typescript
4
4
January 24, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-check-the-type-of-an-object-in-typescript
How to Check the Type of an Object in Typescript ? - GeeksforGeeks
July 23, 2025 - By using the as keyword in the return type of a function, you can create a type predicate that helps TypeScript infer the type of an object.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
In TypeScript, we represent those through object types. ... In all three examples above, we’ve written functions that take objects that contain the property name (which must be a string) and age (which must be a number). We have cheat-sheets available for both type and interface, if you want a quick look at the important every-day syntax at a glance. Each property in an object type can specify a couple of things: the type, whether the property is optional, and whether the property can be written to.
🌐
Turing
turing.com › kb › check-type-of-objects-variables-in-typescript
How to Check Type of Objects & Variables in Typescript
Knowing how to check the type of variables and objects is important when working with TypeScript, and with this simple how-to guide, you will be able to use the typeof operator, instanceof operator, and the in operator to get the types of your ...
🌐
Medium
medium.com › ovrsea › checking-the-type-of-an-object-in-typescript-the-type-guards-24d98d9119b0
Checking the type of an object in Typescript: the type guards | by Antoine Sauvage | OVRSEA | Medium
January 16, 2019 - Our problem is classic in Typescript: an object type is undifferentiated, and we would like to differentiate the different cases. In the example below, we have two interfaces, Animal and Person: ... If we want to print the name, it’s easy. But if we want to print “cat”, “”dog” or “human”, it becomes more complicated… Below, both functions don’t work: ... The first one because type is not defined, the second one because instanceof works with classes.
Find elsewhere
🌐
W3Schools
w3schools.com › typescript › typescript_object_types.php
TypeScript Object Types
const car: { type: string, model: string, year: number } = { type: "Toyota", model: "Corolla", year: 2009 }; Try it Yourself » · Object types like this can also be written separately, and even be reused, look at interfaces for more details. TypeScript can infer the types of properties based on their values.
🌐
Rostacik
rostacik.net › 2015 › 05 › 27 › how-to-get-type-of-object-in-typescript-when-using-union-types-and-type-guards-for-multiple-types-of-function-parameter-or-variable
How to get type of object in TypeScript, when using union types and type guards for multiple types of function parameter or variable. | rostacik.net dev blog
When checking for primitive types in TypeScript , typeof variable === “string” or typeof variable === “number” should do the job. When checking for HTMLElement or HTMLDivElement type of objects, variable instanceof HTMLElement or variable instanceof HTMLDivElement type of check should ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › typeof
typeof - JavaScript - MDN Web Docs - Mozilla
May 22, 2026 - The typeof operator returns a string indicating the type of the operand's value. console.log(typeof 42); // Expected output: "number" console.log(typeof "blubber"); // Expected output: "string" console.log(typeof true); // Expected output: "boolean" console.log(typeof undeclaredVariable); // ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-get-type-of-object
Understanding TypeScript: Getting the Type of an Object
One of the simplest ways to get the type of an object in TypeScript is by using the typeof operator. This operator returns the type of a variable, providing valuable information about the data structure.
🌐
DEV Community
dev.to › andreasbergqvist › typescript-get-types-from-data-using-typeof-4b9c
JavaScrpt typeOf: TypeScript - get types from data using typeof - DEV Community
April 10, 2019 - Since TypeScript 3.4 you can do the following if you have an array of strings (notice the as const): const data = ['text 1', 'text 2'] as const; type Data = typeof data[number]; // type Data = "text 1" | "text 2" It's also possible to get types from arrays with objects:
🌐
TypeScript Tutorial
typescripttutorial.net › home › typescript tutorial › typescript object type
TypeScript object Type
October 18, 2024 - TypeScript has another type called Object with the letter O in uppercase. It’s important to understand the differences between them. The object type represents all non-primitive values while the Object type describes the functionality of all objects.
🌐
xjavascript
xjavascript.com › blog › typescript-get-type-of-object
Understanding How to Get the Type of an Object in TypeScript — xjavascript.com
Keep your interfaces and type aliases focused on the properties and behaviors relevant to the object. When working with union types, type guards are the best way to narrow down the type and access the properties specific to each type. Take advantage of TypeScript's static analysis to catch type-related errors at compile-time. This can save a lot of debugging time. Getting the type of an object in TypeScript is a crucial skill for writing robust and maintainable code.
🌐
Tim Mousk
timmousk.com › blog › typescript-check-type
How To Check The Type In TypeScript? – Tim Mouskhelichvili
March 8, 2023 - To check the type of a class in TypeScript, you can use the instanceof operator. This operator returns a boolean (true or false) and tests if an object is of the specified object type.
🌐
Maya Shavin
mayashavin.com › articles › types-from-constants-typescript
Using keyof and typeof for types efficiently in TypeScript
February 15, 2023 - When hovering on myName in your IDE, TypeScript will show you the type for myName concluded from Name as in the following screenshot: Note here with typeof, if the variable to extract type is an Object, the type received will be a complete type structure, and each property has its type (such as type of myName will have two fields - firstName of type string, and lastName of kind string).
🌐
Mimo
mimo.org › glossary › typescript › object-type
TypeScript Object Type: Syntax, Usage, and Examples
The TypeScript object type allows you to define the structure and shape of an object with static typing. It helps you write code that checks for key names and value types before runtime.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-get-type-of-an-object
Understanding TypeScript: Get Type of an Object
In this blog post, we will explore different ways to achieve this and enhance your TypeScript skills. One common way to get the type of an object in TypeScript is by using the typeof operator. This operator returns the type of a variable, property, or expression.