Skip to content

Partial inconsistent behavior with undefined types #18823

Closed
@deregtd

Description

@deregtd

We're trying to figure out the right way to deal with potential react issues with typings. We've recently migrated our enormous codebase over to using TS 2.6 and enabled strictNullChecks (after working through over 7 thousand compile errors!) and found that we still have some bugs due to typescript allowing nulls to make their way into state through what feels to me like a bug. Someone complained about a similar problem with #12793 but it sounds like the behavior they were talking about could be argued about. However, I have what feels more like a contradiction in the compiler.

interface a { 
    x: number;
    y: number|undefined;
}

let z1: a = { x: 4, y: undefined }; // OK -- Makes sense
let z2: a = { x: undefined, y: undefined }; // Error -- x can't be undefined, sure
let z3: a = { x: 4, y: undefined, z: 4 }; // Error -- z isn't on the interface
let z4: Partial<a> = { z: 4, x: undefined };  // Error -- Makes sense, extra z parameter not on interface
let z5: Partial<a> = { x: undefined };  // OK -- **Whoa, what?**

In this example (z5), x is on the interface, but explicitly doesn't allow undefined. Why is it okay to have this "missing parameter" that's outside the interface, but I can't add z:4 to the same Partial? If you switched to allowing z: 4 on the Partial interface, that'd make it a lot less useful, but that would at least be consistent; but the very correct behavior here (and what the common usage of Partial tends to be) feels like banning x: undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions