You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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,
272
272
273
273
```ts
274
-
function find<T, UextendsFindable<T>>(n:T, s:U) {// errors because type parameter used in constraint
274
+
function find<T, UextendsFindable<T>>(n:T, s:U) {
275
275
// ...
276
276
}
277
277
find (giraffe, myAnimals);
278
278
```
279
279
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
-
291
280
## Using Class Types in Generics
292
281
293
282
When creating factories in TypeScript using generics, it is necessary to refer to class types by their constructor functions. For example,
0 commit comments