Skip to content
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

The info that generic type extends some type is lost during inheritance #18156

Closed
astashov opened this issue Aug 30, 2017 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@astashov
Copy link

TypeScript Version: 2.4.2

Code

class Value<T> {
  constructor(protected readonly data: T) {}

  public bar(data: Partial<T>): this {
    return this;
  }
}

interface Type {
  foo: string;
}

class TheClass<T extends Type> extends Value<T> {
  public foo(): this {
    return this.bar({foo: "d"});
  }
}

Expected behavior:

No errors, since it should be aware T extends Type, which has foo.

Actual behavior:

Shows an error:

Argument of type '{ foo: "d"; }' is not assignable to parameter of type 'Partial<T>'.

Playground

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 31, 2017
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 31, 2017

Seems related to #16985. Can you take a look @ahejlsberg?

@astashov
Copy link
Author

I think it happened before 2.4 as well.

@DanielRosenwasser
Copy link
Member

Actually this is technically correct behavior, not sure why I didn't catch that the first time.

@Jessidhia
Copy link

Jessidhia commented Oct 30, 2017

Why is this correct? Changing the class to class TheClass extends Value<Type> does correctly accept the Partial<Type> as an argument; <T extends Type> should have T be equivalent to Type for template type checking purposes, no?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

{foo: "d"} is not assignable to Partial<T>, T is generic. see #19388 for an explanation.

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Nov 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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

4 participants