Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Update Generics.md #270

Merged
merged 1 commit into from
May 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions pages/Generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,26 +268,15 @@ loggingIdentity({length: 10, value: 3});

## Using Type Parameters in Generic Constraints

In some cases, it may be useful to declare a type parameter that is constrained by another type parameter. For example,
You can declare a type parameter that is constrained by another type parameter. For example,

```ts
function find<T, U extends Findable<T>>(n: T, s: U) { // errors because type parameter used in constraint
function find<T, U extends Findable<T>>(n: T, s: U) {
// ...
}
find (giraffe, myAnimals);
```

You can achieve the pattern above by replacing the type parameter with its constraint. Rewriting the example above,

```ts
function find<T>(n: T, s: Findable<T>) {
// ...
}
find(giraffe, myAnimals);
```

*Note:* The above is not strictly identical, as the return type of the first function could have returned `U`, which the second function pattern does not provide a means to do.

## Using Class Types in Generics

When creating factories in TypeScript using generics, it is necessary to refer to class types by their constructor functions. For example,
Expand Down