Skip to content

Seemingly invalid error on object that correctly provides a partial generic interface. #27419

Closed
@Anupheaus

Description

@Anupheaus

TypeScript Version: 3.1.1-insiders.20180925

Search Terms:
is not assignable to, partial

Code

interface IItem {
  id: string;
  isOpen: boolean;
}

interface IRecord {
  id: string;
}

type Upsertable<T> = Partial<T> & { id: string; };

class A<T extends IItem = IItem> {
  protected setItem(partialItem: Upsertable<T>): void {

  }
}

interface IMyItem extends IItem {
  title: string;
}

class B<T extends IMyItem> extends A<T> {
  constructor() {
    super();
    const id: string = 'uniqueId';
    const isOpen: boolean = false;
    this.setItem({ id, isOpen }); // error here: 
    // Argument of type '{ id: string; isOpen: boolean; }' is not assignable to parameter of type 'Upsertable<T>'.
    // Type '{ id: string; isOpen: boolean; }' is not assignable to type 'Partial<T>'.
  }
}

Expected behavior:
The given properties are valid with the type IMyItem, which in turn extends from IItem, and as all properties bar id are optional, having only isOpen and id should be fine.

Actual behavior:
Errors with Argument of type '{ id: string; isOpen: boolean; }' is not assignable to parameter of type 'Upsertable<T>'. Type '{ id: string; isOpen: boolean; }' is not assignable to type 'Partial<T>'.

Playground Link:
http://www.typescriptlang.org/play/#src=interface%20IItem%20%7B%0D%0A%20%20id%3A%20string%3B%0D%0A%20%20isOpen%3A%20boolean%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20IRecord%20%7B%0D%0A%20%20id%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20Upsertable%3CT%3E%20%3D%20Partial%3CT%3E%20%26%20%7B%20id%3A%20string%3B%20%7D%3B%0D%0A%0D%0Aclass%20A%3CT%20extends%20IItem%20%3D%20IItem%3E%20%7B%0D%0A%20%20protected%20setItem(partialItem%3A%20Upsertable%3CT%3E)%3A%20void%20%7B%0D%0A%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20IMyItem%20extends%20IItem%20%7B%0D%0A%20%20title%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Aclass%20B%3CT%20extends%20IMyItem%3E%20extends%20A%3CT%3E%20%7B%0D%0A%20%20constructor()%20%7B%0D%0A%20%20%20%20super()%3B%0D%0A%20%20%20%20const%20id%3A%20string%20%3D%20'uniqueId'%3B%0D%0A%20%20%20%20const%20isOpen%3A%20boolean%20%3D%20false%3B%0D%0A%20%20%20%20this.setItem(%7B%20id%2C%20isOpen%20%7D)%3B%0D%0A%20%20%7D%0D%0A%7D

Related Issues:
Maybe this bug: #12731?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions