Skip to content

Conditional types works incorrectly when generics with constraints #24190

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
PomanoB opened this issue May 17, 2018 · 2 comments
Closed

Conditional types works incorrectly when generics with constraints #24190

PomanoB opened this issue May 17, 2018 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@PomanoB
Copy link

PomanoB commented May 17, 2018

TypeScript Version: 2.8.1, 2.9.0-dev.20180516

Search Terms: Conditional Types, generics

Code
strictFunctionTypes: true

type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;

interface Interface1
{
}

class Class1<T extends Interface1>
{
    private prop1: NonFunctionProperties<T>|null = null;

    public method1(): Class1<Interface1>
    {
        return this; // <- error TS2322
    }
}

Expected behavior:
No error

Actual behavior:

Error:(15, 9) TS2322: Type 'this' is not assignable to type 'Class1<Interface1>'.
  Type 'Class1<T>' is not assignable to type 'Class1<Interface1>'.
    Type 'Interface1' is not assignable to type 'T'.

Playground Link:
Need to enable strictFunctionTypes in Options
Playground

Related Issues:

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 18, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 18, 2018

This is the result of --strictFunctionTypes class Class1 is contravariant with respect to T, since T is used in contravarient location (i.e. keyof T). The result is that Interface1 needs to be assingbale to this, which is not true.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants