1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-08-01 06:50:26 +02:00

Fix instanceOf mistake (#4322)

instanceof is a runtime check and interface and types don't exist during runtime.

Also TypeScript has a structural type system, which means that they are matched according to the structure of the object and types - not according to instances.

For example:

interface Person {
    name: string;
    age: number
}

const person = {
    name: "Ken",
    age: 25
}

if (person instanceof Person) // Error
This commit is contained in:
Abdul Wahab
2023-08-10 14:14:40 +02:00
committed by GitHub
parent 08e29c2c14
commit ea70632de1

View File

@@ -1,6 +1,6 @@
# instanceOf operator
The `instanceof` operator is a way to narrow down the type of a variable. It is used to check if an object is an instance of a class, interface, or type.
The `instanceof` operator is a way to narrow down the type of a variable. It is used to check if an object is an instance of a class.
```typescript
class Bird {