> For the complete documentation index, see [llms.txt](https://guide-angular.wishtack.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide-angular.wishtack.io/typescript/duck-typing.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://guide-angular.wishtack.io/typescript/duck-typing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
