Skip to content

Commit e6c9160

Browse files
committed
Update Generics.md
Constraining a type parameter using another type parameter is allowed as of 1.8. Fixes microsoft#267.
1 parent 79e1f21 commit e6c9160

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

pages/Generics.md

+2-13
Original file line numberDiff line numberDiff line change
@@ -268,26 +268,15 @@ loggingIdentity({length: 10, value: 3});
268268

269269
## Using Type Parameters in Generic Constraints
270270

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

273273
```ts
274-
function find<T, U extends Findable<T>>(n: T, s: U) { // errors because type parameter used in constraint
274+
function find<T, U extends Findable<T>>(n: T, s: U) {
275275
// ...
276276
}
277277
find (giraffe, myAnimals);
278278
```
279279

280-
You can achieve the pattern above by replacing the type parameter with its constraint. Rewriting the example above,
281-
282-
```ts
283-
function find<T>(n: T, s: Findable<T>) {
284-
// ...
285-
}
286-
find(giraffe, myAnimals);
287-
```
288-
289-
*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.
290-
291280
## Using Class Types in Generics
292281

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

0 commit comments

Comments
 (0)