-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Don't mention constraints in errors when type parameters don't explicitly specify them #33436
Comments
I see the same error in this playground. I am at a loss to what the error is and where |
Looks like the default constraint is coming through as |
i’m seeing this in many places in many different projects |
i have no idea what the error message means |
I had the same question. It was changed to |
I debugged this and the summary is that const noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); Changing this type to be defined as PR: #33445 |
On the topic of error messages: A proposal:
|
@dagda1 Sorry, to answer you questions. Assigning a concrete type to a generic type parameter is incorrect because the type parameter can always be instantiated to some arbitrary type: function f<T>(x: T) {
x = {};
}
f(3);
f(true);
f("something else"); In the body of the function you have no control over the instantiation by the calling context, so we have to treat things of type A common mistake or misunderstanding was to constraint a type parameter and then assign to its constraint, for example: function f<T extends boolean>(x: T) {
x = true;
}
f<false>(false); This is still incorrect because the constraint only restricts the upper bound, it does not tell you how precise To try and address this misunderstanding the additional error elaboration was provided, but in the most degenerate case, an unconstrained parameter, the elaboration is really just noise. The |
@jack-williams thank you for the explanation although I see this error a crazy amount of times now and the implicit The example in this playground on line 34 has nothing to do with assigning to a constraint and I would really appreciate an explanation of what the error message is trying to tell me here. I have so many other examples. |
Line 34 is trying to assign The error message is correct though, and symptomatic of the fact that |
@jack-williams I semi understand why this change was made, but it's not clear how to resolve it. Most of the documentation in this repo seem to say the same thing, but the error does not give a suggestion on how to resolve this. |
@theseyi It's not clear to me why Every const a: A = new B();
const three: 3 = a.methodA(3); Because It's hard for me to help you fix this because I'm not sure what you're trying to achieve, but having a parent method return a generic type, and a child method return a non-generic type, is usually wrong. |
Thanks for responding, I agree in attempting to get to the salient parts of the issue, I may have fuzzied the example and concrete values make it unclear. In other words, I have a class that is generic over a base type (because as you mention inherited and base methods have to be generic), and a subclass that is generic over a more specialized extension of that base type. |
@DanielRosenwasser I have a fix (#33445) that removes the useless error message. Is the proposed message final and OK to go ahead and add? |
Current
Nobody ever mentioned a constraint or
{}
in this code, so why is it being displayed? As a user, this is telling me more than I needed or wanted to know.Proposed
The text was updated successfully, but these errors were encountered: