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

Generic checking error #18387

Closed
tinganho opened this issue Sep 11, 2017 · 4 comments
Closed

Generic checking error #18387

tinganho opened this issue Sep 11, 2017 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@tinganho
Copy link
Contributor

tinganho commented Sep 11, 2017

I think #16368 broke this code for me:

class Model {}

class Collection<M extends Model> {
    m: M;
}

class User extends Model {
}

class Users extends Collection<User> {
}

type CollectionClass = typeof Collection;
let a: CollectionClass = Users; // Error 'User' is not assignable to 'M' 

If I provide the --noStrictGenericChecks flag, it compiles without any complaints. Can I correct this without using the flag?

@mhegazy
Copy link
Contributor

mhegazy commented Sep 11, 2017

#16368 is about generic signatures, and i do not see any generic signatures in the code sample in the OP. so please share some context on what is failing, what is the error that is reported, and why do you believe the error is incorrect.

Before #16368, when generic signatures where compared, all generic parameters were erased to any then the comparison is done. #16368 adds a correct and more strict comparison by instantiating the source in the generic type arguments of the target before doing the check.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Sep 11, 2017
@tinganho
Copy link
Contributor Author

Sorry forgot to include the errors. I updated the OP.

User extends Model, and M is constrained to be at least Model. I think it is weird why User is not assignable to M?

Here is a playground example:
http://www.typescriptlang.org/play/index.html#src=class%20Model%20%7B%7D%0A%0Aclass%20Collection%3CM%20extends%20Model%3E%20%7B%0A%20%20%20%20m%3A%20M%3B%0A%7D%0A%0Aclass%20User%20extends%20Model%20%7B%0A%7D%0A%0Aclass%20Users%20extends%20Collection%3CUser%3E%20%7B%0A%7D%0A%0Atype%20CollectionClass%20%3D%20typeof%20Collection%3B%0Alet%20a%3A%20CollectionClass%20%3D%20Users%3B

#16368 introduced the compilation flag --noStrictGenericChecks. So I would assume it introduced the breaking change. Because when I compile with that flag, no errors occurs.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 11, 2017

here is a simpler repro:

//type CollectionClass = typeof Collection;
type CollectionClass = { new <M extends Model>(): Collection<M> };
type UserCollectionClass = { new(): Collection<User> };

declare let a: CollectionClass;
declare let u: UserCollectionClass;

a = u;  // User is not assignable to M

the check here is telling you that you are replacing a generic function (constructor function for Collection<M>) with one that is more specific one (constructor function for UserCollection ) which is not allowed.

@tinganho
Copy link
Contributor Author

OK I see, thanks for the explanation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants