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
You can declare a type parameter that is constrained by another type parameter.
272
-
For example, here we'd like to take two objects and copy properties from one to the other.
273
-
We'd like to ensure that we're not accidentally writing any extra properties from our `source`, so we'll place a constraint between the two types:
272
+
For example, here we'd like to get a property from an object given its name.
273
+
We'd like to ensure that we're not accidentally grabbing a property that does not exist on the `obj`, so we'll place a constraint between the two types:
274
274
275
275
```ts
276
-
function copyFields<TextendsU, U>(target:T, source:U):T {
277
-
for (let id insource) {
278
-
target[id] =source[id];
279
-
}
280
-
returntarget;
276
+
function getProperty<T, KextendskeyofT>(obj:T, key:K) {
0 commit comments