# Structural typing

**Definition:**

> In order to determine if two types are assignable, the compiler exhaustively compares all their properties.

This contrasts with *nominal typing*, which works like:

> Two types only are assignable if they were created from the same constructor OR from an explicitly related constructor. (explicitly related usually means: extends or implements).

Given two classes A and B:

```typescript
class A {
    name
    lastName
}

class B {
    name
    lastName
    age
}
```

Now let a function require A as input.

```typescript
function requireA(person: A) {}
requireA(new A()) //ok
requireA(new B()) //ok
requireA({ name: 'Barbra', lastName: 'Streisand' }) //ok
requireA({ name: 'Barbra', lastName: 'Streisand', age: 77 }) //error
```

* The function accepted B as input since its properties were considered assignable;
* This would not be allowed on *nominal typing*, since it would require `B` to explicitly `extend` or `implement` `A`;
* Since we are just comparing properties, just directly passing a conforming object also works;
* The last line errors because TS applies a special rule which enforces *exact properties* if the argument is a literal;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wkrueger.gitbook.io/typescript/structural-typing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
