-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Confusing difference between types and interfaces when intersected with string indexes #24970
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
Comments
Intersection is a higher-order process that can occur during e.g. generic instantiation; intentionally this process can never create errors.
|
@RyanCavanaugh I'm not sure I completely understand you. Both |
One of the type rules is that |
ok, so - given that the above behavior is something I've noticed, I believe, but not seen described anywhere with some convincing reasoning.
(at the core of these questions is a deep confusion about the minute differences between |
I am confused like @ekilah on this one. The interface in that situation has one of the constituents assignable to |
The spec defines the rules: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md There is not a single place that explains all emergent behavior. Sometimes we define that X causes Y, and B causes C, and P causes Q, and M causes N, then someone notices that X + B - P + Q isn't identical to X + B - P + M even though M and Q are supposed to be pretty similar - it isn't so much that we specifically designed the X + B - P + M rule and have written down a convincing explanation of why that should be the case, but rather than the initial five propositions are all individually sensible and cannot be changed without making a lot of other things worse. |
I also have been baffled by this distinction. To give another explanation on @MrDesjardins and @ekilah questions: My guess is that type literals work in this case, because they get assigned an implicit index signature, whereas interfaces index signatures have to be declared explicitly. Test case:
So,
|
@Ford04 that's an interesting angle, and I really appreciate you sharing it. Your explanation goes above and beyond any of @RyanCavanaugh 's three "attempts" to half-answer this question or provide some insight after already closing it as if it were obvious. I haven't seen mention of this "implicit index signature" before, but that issue you linked seems to describe a similar issue. Unfortunately it seems like people there have mostly been left to figure it out themselves as well. For what it's worth, to this day I've never found a good resource that actually describes how types and interfaces differ internally or how we, as external clients of the language, should use them differently, and after using TypeScript for 1.5 years professionally now I think that is simply ridiculous. Here we are, trying to use the language as intended, and when we run across weird behaviors that don't make a lot of sense, the answer is not much more than a "working as intended" label. @RyanCavanaugh I invite you and your team back to read this thread all the way through to see if you can give us a little better of an answer, all this time later. Individually, none of your answers above make much sense, and together, they sound fairly incoherent. With all the advances of the type system lately, it surprises me that these types of questions are still left mostly unanswered. |
TypeScript Version: 2.8.3
Search Terms: intersection types and interfaces seem to behave differently. Inconsistent 'property 'x' of type 'y' is not assignable to string index type
Code
Expected behavior:
a type error should happen with both,
because
Date
is not inIJSONObject
's index signatureActual behavior:
WithType
doesn't cause an error, even thoughDate
shouldn't be allowed.Playground Link: link
Related Issues: possibly #18075 but I'm not really sure.
The text was updated successfully, but these errors were encountered: