Peter Peter - 4 years ago 211
TypeScript Question

Typeof/instanceof type alias

I wonder if it is possible to determine the type of an object in typescript. Please consider the example below:

type T = [number, boolean];

class B {
foo: T = [3, true];

bar(): boolean {
return instanceof T;

The typeof operator does not seem to be a solution, neither does instanceof.

Answer Source

Short answer

(Almost) all type information are removed after compilation, and you can't use instanceof operator with an operand (T in your example) that does not exist at runtime.

Long answer

An identifier in TypeScript could belong to one or more of the following groups: type, value, namespace. What get's emitted as JavaScript is identifiers in the group of value.

Thus runtime operators only works with values. So if you want to do runtime type checking of the value of foo, you'll need to do the hard work yourself.

See this section for more information:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download