-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Constraint failing for subtype (regression) #29464
Comments
The access here is unsound; I think it was fixed by #27490. Consider the instantiation: type Base = { x: "x" };
type Key = "x";
type Access = Base[Key]; // "x"
func1String<Base, Key>("x"); Here the type of |
What you say makes sense but then, should changing the type of the properties of Base to string should work (or is "extends" preventing it to work) ?
Also why in that case, this example work?
This could clearly have "Key" being "Y" and so the type would not be "string" but "number" |
Changing the properties to The constraint type Base = { x: "x" }; Similarly, because the constraint is only an upper bound, the only thing you can say about The reason that your later example works is because you are not instantiating func1<myInterface1, string, keyof myInterface1>
Basically the difference boils down to where generic parameters appear in an
In general you can reason about generic parameters on the left hand side of a constraint, but it's hard to reason about generic things on the right hand side. |
I kind of understand, but I'm not sure how I can replace my solution with something that works on 3.2+ This is a more complete example:
It worked fine with 3.1 but I don't know how to make it on 3.2 |
Does this work? function validateAttribute<Base extends Record<Key,T>, Key extends keyof Base, T = Base[Key]>(
obj: Base,
attrName: Key,
attrType: TypeName<T>
) {
return typeof obj[attrName] === attrType;
}
function validateString<Base extends Record<Key, string>, Key extends keyof Base>(
obj: Base,
attrName: Key
) {
validateAttribute<Base, Key, string>(obj, attrName, "string");
} Not sure it's the best way though.. |
In this case it works. |
TypeScript Version:
Failing in 3.3.0-dev.20190117
Last version 3.1.6 passing the test
Search Terms:
satisfy constraint subtype FilterFlags
Code
Expected behavior:
Compilation should pass (like it does on 3.1.6)
Actual behavior:
Compilation fails
Playground Link:
http://www.typescriptlang.org/play/#src=function%20func1%3CBase%20extends%20%7B%20%5Bkey%3A%20string%5D%3A%20any%20%7D%2C%20Type%20extends%20Base%5BKey%5D%2C%20Key%20extends%20keyof%20Base%3E(attrName%3A%20Key%2C%20attrType%3A%20Type)%20%7B%0D%0A%20%20%20%20console.log('func1')%0D%0A%7D%0D%0A%0D%0A%0D%0Afunction%20func1String%3CBase%20extends%20%7B%20%5Bkey%3A%20string%5D%3A%20any%20%7D%2C%20Key%20extends%20keyof%20Base%3E(attrName%3A%20Key)%20%7B%0D%0A%20%20%20%20func1%3CBase%2C%20string%2C%20Key%3E(attrName%2C%20%22string%22)%3B%0D%0A%7D
Related Issues:
The text was updated successfully, but these errors were encountered: