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.
type Fish = { swim(): void }; type Bird = { fly(): void }; function move(a: Fish | Bird) { if ("swim" in a) a.swim(); else a.fly(); }
Try it
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.
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
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 thatstringis not assignable toobject) and aTypeErrorat run time. inincludes inherited properties, so"toString" in {}istrue. UseObject.hasOwnfor 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.
inon an unknown value only narrows toobject & Record<"key", unknown>(TypeScript 4.9+); verify the property's type separately.- Array indexes are keys too:
0 in [1]istrue,1 in [1]isfalse.
Usage notes
- Use
infor 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
into narrow. Checking a name no member declares narrows toneverin some positions. #priv in objis 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. indoes not look at values: a property set toundefinedis stillinthe object.