Skip to content

Generic type extended in method not inferred without cast #35288

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
connor4312 opened this issue Nov 22, 2019 · 2 comments
Closed

Generic type extended in method not inferred without cast #35288

connor4312 opened this issue Nov 22, 2019 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@connor4312
Copy link
Member

connor4312 commented Nov 22, 2019

TypeScript Version: 3.8.0-dev.20191121

Search Terms: unknown' is not assignable to type, interface cast, unknown generic

Code

interface IFoo<T> {
    run<R extends T>(value: R): R;
}

class Foo<T> implements IFoo<T> {
    run<R extends T>(value: R): R { return value; }
}

declare function bar<T>(foo: IFoo<T>): void;

const works: IFoo<string> = new Foo<string>();
const doesNotWork = new Foo<string>();

bar(works);
bar(doesNotWork);

Expected behavior: I should expect bar to correctly infer the generic type in IFoo when given a Foo<string>

Actual behavior:

Argument of type 'Foo<string>' is not assignable to parameter of type 'IFoo<unknown>'.
  Types of property 'run' are incompatible.
    Type '<R extends string>(value: R) => R' is not assignable to type '<R extends unknown>(value: R) => R'.
      Types of parameters 'value' and 'value' are incompatible.
        Type 'R' is not assignable to type 'string'.
          Type 'unknown' is not assignable to type 'string'.

Playground Link: Playground Link

Related Issues:

@connor4312 connor4312 changed the title Generic type extended in method not without case Generic type extended in method not inferred without cast Nov 22, 2019
@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 8, 2020
@RyanCavanaugh
Copy link
Member

Generic constraints aren't inference sites (and really shouldn't be), so this is effectively https://github.com/microsoft/TypeScript/wiki/FAQ#why-doesnt-type-inference-work-on-this-interface-interface-foot--

@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