-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Issue a custom error message when trying to assign constraint type to generic type parameter #29049
Comments
Very much agree with the suggestion. To throw my unicycle into the arena:
|
Agree with @jack-williams since this can happen in a class too. I think the following combines the best of both:
Also, when not assignable, or when there's an implicit constraint, but the RHS is not instantiable, I could see an elaboraron like
Or something like that. |
I think the problem with could be instantiated with a different type is that the user reaction would probably be "You mean the constraint doesn't do anything?". What about:
|
That's a good point @RyanCavanaugh. I'm not sure if I've substituted in the placeholders correctly; is this right?
In that instance I would probably make it explicit that boolean is the constraint, as I feel it pops out of nowhere abit. So:
|
❤️ ! |
@DanielRosenwasser Seems, that the error needs to add in the method |
Since you're just specializing the error, |
@a-tarasyuk I think |
@RyanCavanaugh |
…eneric type parameter Fixes microsoft#29049. This also adds the new message in chained error messages. `typeParameterDiamond4.errors.txt` shows it appearing twice in the "diamond" scenario. I can't tell if this severely increased amount of nested messages is good or bad...?
@a-tarasyuk are you still interested in this? I'm interested in it going in, but don't want to snatch it out from under you. 😊 |
@JoshuaKGoldberg Never mind 😃. I wanted to make PR at the end of this week, however, I see you have started work and ready to send PR. I'll take another issue 👍 . |
@a-tarasyuk no-no, this one's all you! I'll wait a week before looking into it. |
Current message: [ts] Type 'false' is not assignable to type 'T'. [2322] |
@moccaplusplus I agree that there is a fine balance between being informative and concise. IMO the extra information is helpful; as you can see I've been collating issues that trigger this error, so it's something people definitely come across. Generics are a hard subject, subtyping is a hard subject, and variance is a hard subject---this error is a combination of all three. I think spelling out the problem in a more concrete way is a definite improvement. |
@JoshuaKGoldberg your commit there is looking pretty nice; is there an associated PR? |
Oh hey I forgot about this thread! Thanks for the reminder. Will send now and fix up merge conflicts soon. Maybe "Although " should be prepended to the front of the message to make it more clear it's useful? |
…eneric type parameter (#30394) * Added custom error message when trying to assign constraint type to generic type parameter Fixes #29049. This also adds the new message in chained error messages. `typeParameterDiamond4.errors.txt` shows it appearing twice in the "diamond" scenario. I can't tell if this severely increased amount of nested messages is good or bad...? * Updated diagnostic message per suggestion * Align formatting with local custom
Is it relevant to include some sort of "you should consider doing X, Y or Z to solve this problem" ? It could speed up the process of finding a solution for new comers, as it is not really easy to understand, and there are not many available resources online, I managed to found this : https://stackoverflow.com/a/56701587/6790372 Could you at least modify your first post in this issue, to explain why each case does not work, and what could be done instead ? |
I find this error message undecipherable. I have lots of this type of error message:
The last placeholder is always, the |
I have a question related to this error. I'm trying to implement "Slaying UI antipattern in...", but I'd like to have a separate state for pending but when we already have some data. It gives an error because I use two unrelated generic parameters. Could you help me a bit with how can I constrain Data param to be the same on both success and pendingWithData? |
any workaround? |
@RyanCavanaugh says:
So, did confused reduce after #30394? I highly doubt it. |
Oh, really? Can you give me an example of how's that possible? |
I'm bumping into this a lot recently, and I'm not quite sure how to solve it. Currently trying to do something on top of class SomethingOutsideOfOurControl {
didReceiveResponse<TResult = any>(): Promise<TResult> {
return Promise.resolve({}) as Promise<TResult>
}
}
class Implementation extends SomethingOutsideOfOurControl {
// Property 'didReceiveResponse' in type 'Implementation' is not assignable to the same property in base type 'SomethingOutsideOfOurControl'.
// Type '<T>() => Promise<{ data: any; somethingElse: boolean; }>' is not assignable to type '<TResult = any>() => Promise<TResult>'.
// Type 'Promise<{ data: any; somethingElse: boolean; }>' is not assignable to type 'Promise<TResult>'.
// Type '{ data: any; somethingElse: boolean; }' is not assignable to type 'TResult'.
// 'TResult' could be instantiated with an arbitrary type which could be unrelated to '{ data: any; somethingElse: boolean; }'.(2416)
async didReceiveResponse() {
const data = await super.didReceiveResponse()
return {
data,
somethingElse: true,
}
}
} What would be the correct way of going about things? Seems like I have to throw in a |
My 2cents on this matter is that TS seems to punish you for using default params. Makes sense to me for this to be a warning instead. |
Apparently, TypeScript can't deal with default values for parameters with inferred types: microsoft/TypeScript#29049
@tcodes0 I agree, there seems to be a TypeScript penalty prohibiting use of default values on parameters used to infer generic types (that e.g. then are used to determine a return type, or which are passed through as the generic type parameter to a called function). |
TypeScript Version: master
Search Terms: generic constrained type parameter error
Code
People are regularly confused why code like this doesn't work:
Actual behavior: Unelaborated error like "Can't assign
false
toT
"Expected behavior:
If the source type is assignable to the constraint of a type parameter, we should issue a more specific error that attempts to explain the situation:
🚲 🏠
Playground Link: Link
The text was updated successfully, but these errors were encountered: