Skip to content

'Using Type Parameters in Generic Constraints' code error #13651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leebenson opened this issue Jan 24, 2017 · 1 comment
Closed

'Using Type Parameters in Generic Constraints' code error #13651

leebenson opened this issue Jan 24, 2017 · 1 comment
Labels
Docs The issue relates to how you learn TypeScript

Comments

@leebenson
Copy link

TypeScript Version: 2.1.5

Code

Taken from 'Using Type Parameters in Generic Constraints' in the official docs

function copyFields<T extends U, U>(target: T, source: U): T {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}

let x = { a: 1, b: 2, c: 3, d: 4 };
copyFields(x, { b: 10, d: 20 });

Expected behavior:

Compiles.

Actual behavior:

error TS2322: Type 'U[keyof U]' is not assignable to type 'T[keyof U]'.
Type 'U' is not assignable to type 'T'.

@gcnew
Copy link
Contributor

gcnew commented Jan 24, 2017

I raised the documentation issue in #13023

@mhegazy mhegazy added the Docs The issue relates to how you learn TypeScript label Jan 24, 2017
mhegazy added a commit to microsoft/TypeScript-Handbook that referenced this issue Jan 24, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants