Skip to content

Cannot assert Partial<T>[keyof T] not to be undefined using ! (regression from 2.5) #20673

Closed

Description

TypeScript Version: 2.6.2

Code

See also in the playground example here.

function createComponent<T>(
    comp: T, inputs: Partial<T> = {}) {
    for (const key in inputs) {
        if (inputs.hasOwnProperty(key)) {
            const partialVal = inputs[key];
            const value = partialVal!;
            comp[key] = value;
        }
    }
}

Expected behavior:

parialVal! should strip the |undefined from value, and the assignment should succeed.

Actual behavior:

  Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
    Type 'undefined' is not assignable to type 'T[keyof T]'.
      Type 'undefined' is not assignable to type 'T[string]'.
        Type 'Partial<T>[keyof T]' is not assignable to type 'T[string]'.
          Type 'T[keyof T] | undefined' is not assignable to type 'T[string]'.
            Type 'undefined' is not assignable to type 'T[string]'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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