Skip to content

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

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
Anupheaus opened this issue Sep 28, 2018 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Anupheaus
Copy link

Anupheaus commented Sep 28, 2018

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?

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 28, 2018
@RyanCavanaugh
Copy link
Member

See #13442 or its linked issues

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants