-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
type parameter with a constraint on an indexed type cannot be indexed #31726
Comments
Hmm, this is an interesting case. Subtyping ( The interesting thing is that the assignment is unsound: We don't know that function foo<E extends { pig: string }>(e: E) {
e.pig = "oh no"; // this checks out
}
let x = { pig: "it eats people" } as const;
foo(x); |
This is working as intended. This behavior was introduced by #30769 |
Hmm, #30769 was a broader change than I realized. Is there a case where the code above (or something like it) wouldn’t be typesafe? You can’t remove properties from a subtype (otherwise it’s not a subtype anymore), so AFAICT |
That's not the case. It only enforces that all known properties (if any) of the parameter are of type foo({});
foo({x: ''}); |
Hmm, but you can’t actually index the type with arbitrary keys then, can you? So the function would also have to accept one or more Thanks for the explanation! |
thank you guys for helping and referring me to the original PR, this issue #31661 seems to be the same and has some more discussion. |
TypeScript Version: 3.6.0-dev.20190602
Search Terms:
generics, indexed types
Code
Expected behavior:
you should be able to index the
e
function parameter, as was the behavior before 3.5.Actual behavior:
in typescript 3.5 this code results in a
Type '"a"' cannot be used to index type 'E'
error.I understand this is (probably) caused by this change #30637
But since I'm adding a constraint on the type parameter I feel like this still keeps type safety and should be a bug.
(This feels like such an obvious use case I figured there has to be an issue already open or closed and I was really hesitant on opening this issue, but I couldn't find any. If it's a duplicate, sorry...)
The text was updated successfully, but these errors were encountered: