# Duck Typing

On retrouve en TypeScript ce concept très pythonique et pratique.

> If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

... ce à quoi nous pouvons ajouter que si ce n'était pas un canard alors il n'avait qu'à **mieux marquer sa différence** et surtout **ne pas rôder autour des chasseurs**.&#x20;

Autrement dit, si deux objets "matchent" en terme de "duck typing" alors que leurs propriétés n'ont pas du tout la même signification alors cela révèle plutôt un problème de design qu'un problème lié au "duck typing" en lui-même.

Exemple :

```typescript
/* Some user library. */
class User {
    firstName: string;
    lastName: string;
    email: string;
    address: string;
}

/* Some shop administration library. */
class ShopOwner {
    firstName: string;
    lastName: string;
    email: string;
    phoneNumber: string;
}

class Shop {
    email: string;
}

/* Emailing library. */
interface Emailable {
    firstName: string;
    lastName: string;
    email: string;
}

const sendEmail = (message: string, emailable: Emailable) => {
    ...
};

// OK
sendEmail('Hi', new User());
// OK
sendEmail('Hi', new ShopOwner());
// OK
sendEmail('Hi', {
    firstName: 'Foo',
    lastName: 'BAR',
    email: 'foo.bar@wishtack.com'
});

// error TS2345: Argument of type 'Shop' is not assignable to parameter of type 'Emailable'.
//  Property 'firstName' is missing in type 'Shop'.
sendEmail('Hi', new Shop());
```


---

# 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://guide-angular.wishtack.io/typescript/duck-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.
