You cannot do this in a single function declaration. The reason is that extends generic constrain cannot reference other generics in the same group. However, you can divide the function into two parts (currying). Then use the type of the first argument (now in a separate function) to restrict the type of the second argument, as shown below:
var override = <T>(first:T) => <U extends T>(second:U) : T => { let result = <T>{}; for (let id in first) { result[id] = first[id]; } for (let id in second) { result[id] = second[id]; } return result; } override({a:123})({a:123, b:456});
source share