Skip to content

Typechecking Partial<> works differently for generic types #12751

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
jauco opened this issue Dec 8, 2016 · 4 comments
Closed

Typechecking Partial<> works differently for generic types #12751

jauco opened this issue Dec 8, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@jauco
Copy link

jauco commented Dec 8, 2016

TypeScript Version: 2.1.1 (what's running on the playground)

Code
This works

interface Data {
    a?: "a"|"b",
    b: number,
    c: "a" | "b"
}

type Patch = Partial<Data> 
function copyFields(target: Data, source: Patch): Data {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}

function makesrc(): Data { return {b: 1, c: "a"}}


/*1*/copyFields(makesrc(), {d: "d"}); //Should (and does) give an error
/*2*/copyFields(makesrc(), {a: "d"}); //Should (and does) give an error
/*3*/copyFields(makesrc(), {c: "d"}); //Should (and does) give an error

//I don't want to specify all the source properties 
/*4*/copyFields(makesrc(), {b: 2}); //Should not (and doesn't) give an error
/*5*/copyFields(makesrc(), {a: "b"}); //Should not (and doesn't) give an error

But the generic version doesn't

interface Data {
    a?: "a"|"b",
    b: number,
    c: "a" | "b"
}

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

function makesrc(): Data { return {b: 1, c: "a"}}


/*1*/copyFields(makesrc(), {d: "d"}); //Should (and does) give an error
/*2*/copyFields(makesrc(), {a: "d"}); //Should (and does) give an error
/*3*/copyFields(makesrc(), {c: "d"}); //Should (BUT DOES NOT) give an error

//I don't want to specify all the source properties 
/*4*/copyFields(makesrc(), {b: 2}); //Should not (and doesn't) give an error
/*5*/copyFields(makesrc(), {a: "b"}); //Should not (BUT DOES) give an error

I expected no change in type safety in the generic version.

@aluanhaddad
Copy link
Contributor

Error in nightly.

@timocov
Copy link
Contributor

timocov commented Dec 8, 2016

Workaround. Specify type T:

/*3*/copyFields<Data>(makesrc(), {c: "d"});
/*5*/copyFields<Data>(makesrc(), {a: "b"});

@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2016

duplicate of #12633, fixed by #12640. should be working as intended in TS 2.1.4

@mhegazy mhegazy closed this as completed Dec 8, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 8, 2016
@jauco
Copy link
Author

jauco commented Dec 8, 2016

Thanks! Sorry for reporting the duplicate. I found it hard to find the right keywords to search for.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants