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

Commit

Permalink
Merge pull request #270 from spikefoo/update-generics
Browse files Browse the repository at this point in the history
Update Generics.md
  • Loading branch information
DanielRosenwasser committed May 3, 2016
2 parents 76e808f + f79b86b commit c21bc48
Showing 1 changed file with 2 additions and 13 deletions.
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

0 comments on commit c21bc48

Please sign in to comment.