Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The in operator

Reference for the in operator in TypeScript: checking property names at run time, narrowing unions, the prototype chain, and private #name brand checks.

"key" in object returns true when the object has a property with that name, either its own or inherited through the prototype chain. TypeScript treats the check as a narrowing tool: for a union, the true branch keeps the members that could have the property, and the false branch keeps those that could not.

That makes in the natural way to tell apart object types that are not classes (so instanceof does not apply) and do not share a discriminant property.

TypeScript
type Fish = { swim(): void };
type Bird = { fly(): void };
function move(a: Fish | Bird) {
  if ("swim" in a) a.swim();
  else a.fly();
}

Try it

TypeScript Narrowing a union by property

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Unknown values, optional props and private names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
key in object
#privateName in object
Part Description
key A string, number or symbol (or a private name like #x). Converted to a property key.
object Must be of type any, an object type or a type parameter. A primitive is an error at run time and at compile time.
Result boolean. Narrowing happens when key is a string literal (or a literal type).

Edge cases and errors:

  • The right operand must not be a primitive: "a" in "text" is a compile error (TypeScript 7 reports that string is not assignable to object) and a TypeError at run time.
  • in includes inherited properties, so "toString" in {} is true. Use Object.hasOwn for own properties.
  • For a union where a member has the property as optional, both branches keep that member, so check the narrowed value before use.
  • in on an unknown value only narrows to object & Record<"key", unknown> (TypeScript 4.9+); verify the property's type separately.
  • Array indexes are keys too: 0 in [1] is true, 1 in [1] is false.

Usage notes

  • Use in for unions of interfaces and object literal types. If the members share a literal-typed field (a discriminant), compare that field instead; it is more reliable and clearer.
  • A property must be known on at least one union member for in to narrow. Checking a name no member declares narrows to never in some positions.
  • #priv in obj is an ergonomic brand check available in classes; it is true only for real instances that ran the field initializer.
  • The for (const k in obj) loop is a different syntax: it enumerates string keys, including inherited enumerable ones.
  • in does not look at values: a property set to undefined is still in the object.