Article · Wikipedia archive · Last revised May 27, 2026

Typeof

typeof, alternately also typeOf, and TypeOf, is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.

Last revised
May 27, 2026
Read time
≈ 5 min
Length
1,092 w
Citations
16
Source

typeof, alternately also typeOf, and TypeOf, is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.

In languages that support polymorphism and type casting, the typeof operator may have one of two distinct meanings when applied to an object. In some languages, such as Visual Basic,1 the typeof operator returns the dynamic type of the object. That is, it returns the true, original type of the object, irrespective of any type casting. In these languages, the typeof operator is the method for obtaining run-time type information.

In other languages, such as C#2 or D3 and, in C (as of C23),45 the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form. These languages usually have other constructs for obtaining run-time type information, such as typeid.

Such an operator may be usable as a value, providing a run-time representation of the type, or as a type itself that can be applied to a value.

Examples

C

As of C23 typeof is a part of the C standard. The operator typeof_unqual was also added which is the same as typeof, except it removes cvr-qualification and atomic qualification (differentiating it from decltype in C++).67

typeof can be useful for binding to variables of unknown type in macros. In this example, max must use temporary variables to avoid the inputs being evaluated multiple times, and uses typeof to specify the type of these temporary variables. Note that this example uses a non-standard extension to the C language, namely the use of compound statements as expressions (supported e.g. by GNU GCC8)

#define max(a, b) ({ \
    typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    _a > _b ? _a : _b; \
})

C++

C++ provides the decltype and typeid operators.910 decltype, like C’s typeof, is used to abbreviate types and to give types to variables that depend on the type of generic variables, whose type is unknown (e.g. in templates or macros). typeid provides a run-time representation of the type for dynamic dispatch.

struct Double {
    double value;
}

const Double* d;

// decltype can be used in type declarations
decltype(d->value) y; // equivalent to double y;

// decltype can be used in trailing return types
// This may be necessary for overloaded operators where, for instance,
// type T + type U may result in type V distinct from T and U
template <typename T, typename U>
auto add(T t, U u) -> decltype(t + u) {
    return t + u;
}

The ^^ reflection operator, introduced in C++26, can also be used to obtain a representation of a type as a value.11

import std;

using std::string;
using std::meta::info;

struct User {
    string name;
    int age;
};

int main() {
    constexpr info stringClass = ^^string;

    constexpr User john {
        .name = "John Doe",
        .age = 20
    };
    std::println("The class of {} is {}", john.name, std::meta::identifier_of(^^User));
}

C#

In C#, the System.Object.GetType() method returns a System.Type value representing the run-time type of a value.12 The typeof operator instead provides the runtime representation of a type and does not determine the type of a value.13

// Given an object, returns if it is an integer.
// The "is" operator can be also used to determine this.
public static bool IsInteger(object o)
{
    return o.GetType() == typeof(int);
}

Java

java.lang.Object's getClass() method can be used to return the class of any object, which is always an instance of the java.lang.Class class.14 All types can be converted into a run-time representation value by appending ".class". Despite the name, this is possible even if they are not classes, such as primitives (int.class) or arrays (String[].class).

String s = "Hello, world!";
Class<?> sClass = s.getClass();

Class<String> stringClass = String.class;
Class<Integer> intClass = int.class;

Object obj = /* something here */;
System.out.printf("The class of %s is %s%n", obj, obj.getClass().getName());

JavaScript

The typeof operator in JavaScript returns a string representing the basic run-time type of a value.15

function isNumber(n) {
    return typeof n === 'number';
}

TypeScript

TypeScript extends the JavaScript typeof operator, allowing its use in the type of another value, representing the static type of the value.

function someFunction(param: typeof existingObject): void { 
    // ...
}
let newObject: typeof existingObject;

Python

Python has the built-in function type.16

print(type(123))
# prints: <class 'int'>

VB.NET

In VB.NET, the C# variant of "typeof" should be translated into the VB.NET's GetType method. The TypeOf keyword in VB.NET is used to compare an object reference variable to a data type.

The following example uses TypeOf...Is expressions to test the type compatibility of two object reference variables with various data types.

Dim refInteger As Object = 2

MsgBox("TypeOf Object[Integer] Is Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object[Integer] Is Double? " & TypeOf refInteger Is Double)

Dim refForm As Object = New System.Windows.Forms.Form

MsgBox("TypeOf Object[Form] Is Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object[Form] Is Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object[Form] Is Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object[Form] Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
See also

See also

References

References

  1. "TypeOf Operator (Visual Basic)". MSDN. Archived from the original on Nov 28, 2016.
  2. "typeof (C#)". MSDN. Archived from the original on Sep 10, 2016.
  3. "Declarations - D Programming Language 1.0". Digital Mars. Dec 30, 2012. Archived from the original on Oct 7, 2023.
  4. "Typeof" in "Using the GNU Compiler Collection".
  5. Meneide, JeanHeyd (2021-03-07). "Not-So-Magic - typeof(…) in C | r2". Open Standards. Retrieved 2021-12-02.
  6. "N2927: Not-so-magic - typeof for C". Open Standards. 2022-02-02. Archived from the original on Dec 1, 2023.
  7. "Consider renaming remove_quals" (PDF). Open Standards. 2022-02-06. Archived (PDF) from the original on Feb 17, 2024.
  8. "Statement Exprs (Using the GNU Compiler Collection (GCC))". gcc.gnu.org. Retrieved 2026-05-10.
  9. "decltype specifier". cppreference.com. 22 April 2026. Retrieved 10 May 2026.{{cite web}}: CS1 maint: url-status (link)
  10. "typeid operator". cppreference.com. 13 August 2024. Retrieved 10 May 2026.{{cite web}}: CS1 maint: url-status (link)
  11. Wyatt Childers; et al. (26 June 2024). "P2996R4 - Reflection for C++26". isocpp.org. WG21.
  12. Microsoft Learn. "Object.GetType Method". learn.microsoft.com. Microsoft Learn. Retrieved 21 April 2026.
  13. "Type-testing operators and cast expressions test the runtime type of an object - C# reference". learn.microsoft.com. Retrieved 2026-05-10.
  14. Oracle Corporation (21 April 2026). "Class Object". docs.oracle.com. Java SE Documentation.
  15. "typeof - JavaScript | MDN". MDN Web Docs. 2026-01-08. Retrieved 2026-05-10.
  16. "Built-in Functions". Python documentation. Retrieved 20 June 2025.