Skip to content

Assignment of a partial type member to an intersection type including the partial does not compile in 2.7.0-dev.20180112 #21170

Closed
@athasach

Description

@athasach

TypeScript Version: 2.7.0-dev.20180112

Code

interface IX {
    x: boolean;
}

function fn<T>(obj: T, props: Partial<T>): (T & IX) {
    const xobj = obj as T & IX;
    xobj.x = true;
    for (const p in props) {
        // The following line causes a compilation error:
        // TS2322: Type 'Partial<T>[keyof T]' is not assignable to type '(T & IX)[keyof T]'.
        xobj[p] = props[p];
    }
    return xobj;
}

Expected behavior:
This should compile since the members of Partial<T> is a subset of the members of (T & IX).
This does compile in 2.7.0-dev.20180108.

Actual behavior:
The following error is emitted when compiled:

index.ts(9,9): error TS2322: Type 'Partial<T>[keyof T]' is not assignable to type '(T & IX)[keyof T]'.
  Type 'T[keyof T]' is not assignable to type '(T & IX)[keyof T]'.
    Type 'T' is not assignable to type 'T & IX'.
      Type 'T' is not assignable to type 'IX'.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions