# Typeof

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Typeof
> Markdown URL: https://mediated.wiki/source/Typeof.md
> Source: https://en.wikipedia.org/wiki/Typeof
> Source revision: 1353478096
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Programming language operator}}
{{lowercase}}
'''{{mono|typeof}}''', alternately also '''{{mono|typeOf}}''', and '''{{mono|TypeOf}}''', is an [operator](/source/operator_(programming)) provided by several [programming language](/source/programming_language)s to determine the [data type](/source/data_type) of a [variable](/source/variable_(programming)). This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type.

In languages that support [polymorphism](/source/type_polymorphism) and [type casting](/source/type_conversion), the typeof operator may have one of two distinct meanings when applied to an [object](/source/object_(computer_science)). In some languages, such as [Visual Basic](/source/Visual_Basic),<ref>{{cite web |url=https://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx |title=TypeOf Operator (Visual Basic) |website= [MSDN](/source/MSDN) |url-status=dead |archive-url=https://web.archive.org/web/20161128133108/https://msdn.microsoft.com/en-us/library/0ec5kw18(VS.80).aspx |archive-date= Nov 28, 2016 }}</ref> the typeof operator returns the [dynamic type](/source/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](/source/run-time_type_information).

In other languages, such as [C#](/source/C_Sharp_(programming_language))<ref>{{cite web |url=https://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx |title=typeof (C#) |website=MSDN |url-status=dead |archive-url=https://web.archive.org/web/20160910105030/https://msdn.microsoft.com/en-us/library/58918ffs(VS.80).aspx |archive-date= Sep 10, 2016 }}</ref> or [D](/source/D_(programming_language))<ref>{{Cite web|url=http://digitalmars.com/d/1.0/declaration.html#Typeof|title = Declarations - D Programming Language 1.0 |website=Digital Mars |date=Dec 30, 2012 |url-status=live |archive-url= https://web.archive.org/web/20231007004441/https://digitalmars.com/d/1.0/declaration.html#Typeof|archive-date= Oct 7, 2023 }}</ref> and, in C (as of [C23](/source/C23_(C_standard_revision))),<ref name="gcc">"[https://gcc.gnu.org/onlinedocs/gcc/Typeof.html Typeof]" in "Using the GNU Compiler Collection".</ref><ref>{{Cite web|last=Meneide|first=JeanHeyd|date=2021-03-07|title=Not-So-Magic - typeof(…) in C {{!}} r2|url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2685.htm|access-date=2021-12-02|website=Open Standards }}</ref> the typeof operator returns the [static type](/source/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 [{{mono|typeid}}](/source/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](/source/C23_(C_standard_revision)) <code>typeof</code> is a part of the C standard. The operator <code>typeof_unqual</code> was also added which is the same as <code>typeof</code>, except it removes cvr-qualification and atomic qualification (differentiating it from <code>decltype</code> in C++).<ref>{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm|title=N2927: Not-so-magic - typeof for C |date=2022-02-02 |website=Open Standards |url-status=live |archive-url= https://web.archive.org/web/20231201192011/http://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm |archive-date= Dec 1, 2023 }}</ref><ref>{{cite web|url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2930.pdf|title=Consider renaming remove_quals |date=2022-02-06 |website=Open Standards |url-status=live |archive-url=https://web.archive.org/web/20240217091853/http://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2930.pdf |archive-date= Feb 17, 2024 }}</ref>

<code>typeof</code> can be useful for binding to variables of unknown type in macros. In this example, <code>max</code> must use temporary variables to avoid the inputs being evaluated multiple times, and uses <code>typeof</code> 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 GCC<ref>{{Cite web |title=Statement Exprs (Using the GNU Compiler Collection (GCC)) |url=https://gcc.gnu.org/onlinedocs/gcc-16.1.0/gcc/Statement-Exprs.html |access-date=2026-05-10 |website=gcc.gnu.org}}</ref>)
<syntaxhighlight lang="c">#define max(a, b) ({ \
    typeof (a) _a = (a); \
    typeof (b) _b = (b); \
    _a > _b ? _a : _b; \
})</syntaxhighlight>

===C++===
{{Main|decltype}}
C++ provides the <code>decltype</code> and <code>typeid</code> operators.<ref>{{Cite web |date=22 April 2026 |title=decltype specifier |url=https://cppreference.com/cpp/language/decltype |url-status=live |access-date=10 May 2026 |website=cppreference.com}}</ref><ref>{{Cite web |date=13 August 2024 |title=typeid operator |url=https://cppreference.com/cpp/language/typeid |url-status=live |access-date=10 May 2026 |website=cppreference.com}}</ref> <code>decltype</code>, like C’s <code>typeof</code>, 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). <code>typeid</code> provides a run-time representation of the type for dynamic dispatch.

<syntaxhighlight lang="cpp">
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;
}
</syntaxhighlight>

The <code>^^</code> [reflection](/source/Reflection_(programming)) operator, introduced in [C++26](/source/C%2B%2B26), can also be used to obtain a representation of a type as a value.<ref>{{Cite web|title=P2996R4 - Reflection for C++26|url=https://isocpp.org/files/papers/P2996R4.html|author=Wyatt Childers|display-authors=etal|date=26 June 2024|publisher=WG21|website=isocpp.org}}</ref>
<syntaxhighlight lang="cpp">
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));
}
</syntaxhighlight>

===C#===
In [C#](/source/C_Sharp_(programming_language)), the <code>System.Object.GetType()</code> method returns a <code>System.Type</code> value representing the run-time type of a value.<ref>{{Cite web|title=Object.GetType Method|url=https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype|author=Microsoft Learn|publisher=Microsoft Learn|website=learn.microsoft.com|access-date=21 April 2026}}</ref> The <code>typeof</code> operator instead provides the runtime representation of a type and does not determine the type of a value.<ref>{{Cite web |last= |title=Type-testing operators and cast expressions test the runtime type of an object - C# reference |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast |access-date=2026-05-10 |website=learn.microsoft.com |language=en-us}}</ref>
<syntaxhighlight lang="c#">
// 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);
}
</syntaxhighlight>

===Java===
<code>java.lang.Object</code>'s <code>getClass()</code> method can be used to return the class of any object, which is always an instance of the <code>java.lang.Class</code> class.<ref>{{Cite web|title=Class Object|url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Object.html#getClass()|publisher=Java SE Documentation|author=Oracle Corporation|website=docs.oracle.com|date=21 April 2026}}</ref> All types can be converted into a run-time representation value by appending "<code>.class</code>". Despite the name, this is possible even if they are not classes, such as primitives (<code>int.class</code>) or arrays (<code>String[].class</code>).

<syntaxhighlight lang="java">
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());
</syntaxhighlight>

===JavaScript===
The <code>typeof</code> operator in JavaScript returns a string representing the basic run-time type of a value.<ref>{{Cite web |date=2026-01-08 |title=typeof - JavaScript {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof |access-date=2026-05-10 |website=MDN Web Docs |language=en-US}}</ref><syntaxhighlight lang="javascript">
function isNumber(n) {
    return typeof n === 'number';
}
</syntaxhighlight>

===TypeScript===
TypeScript extends the JavaScript <code>typeof</code> operator, allowing its use in the type of another value, representing the static type of the value.
<syntaxhighlight lang="typescript">
function someFunction(param: typeof existingObject): void { 
    // ...
}
</syntaxhighlight>
<syntaxhighlight lang="typescript">
let newObject: typeof existingObject;
</syntaxhighlight>

===Python===
[Python](/source/Python_(programming_language)) has the built-in function {{code|type}}.<ref>{{cite web |title=Built-in Functions |url=https://docs.python.org/3/library/functions.html#type |website=Python documentation |access-date=20 June 2025 |language=en}}</ref>
<syntaxhighlight lang="python">
print(type(123))
# prints: <class 'int'>
</syntaxhighlight>

===VB.NET===
In [VB.NET](/source/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.<syntaxhighlight lang="vb.net">
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)
</syntaxhighlight>

==See also==
*[sizeof](/source/sizeof)
*[decltype](/source/decltype)
*[Type introspection](/source/Type_introspection)

==References==
{{Reflist}}

Category:Operators (programming)

---
Adapted from the Wikipedia article [Typeof](https://en.wikipedia.org/wiki/Typeof) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Typeof?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
