misleading error message on invalid assignment when one type parameter extends another #60881
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
🔎 Search Terms
generic, type parameter, extends, arbitrary type which could be unrelated, assignment
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?target=99&ts=5.8.0-dev.20241229&noUncheckedIndexedAccess=false#code/GYVwdgxgLglg9mABABwIYCdUFsCmUfoCiAHvmACYDOA8lABYEAKG2eBAPAMojIEAqAT14AaRNwBGg3ohykcFSmJ78hOAHwAKSsvRScALiW9dq0dsmrDEvQEpEAbwBQiRAHpXiPp0QBmAHQ+hnqIAOTcxnohiDCKYHBQiKiUlDAA5mCo4gA2OIhQcHmqodaqIX7Obq4V7p7e-gCsQUVhOpHRsfGJyWkZ2bn5hdItFrxliNUeLlNTLRGl7V0p6Zk5eQX0uRAIlFCYMGAJcMCDucORouIgCWfzWyBZ5Ijiufs7qAcwqPiPAO4w9IlEOQYMBgAR5AlzFAikdEFswDs9gdQvYAL5lCYTWq+PwATiaQ3CKlGCziCSSS16qwG0MJIBGOAxLhq02mNxJdweTxeCKg71gXxwv3+dESSAw4n+mHQAhOiB+dBgEFFnMez0Q4HQOCygseA1mxMZ5WZVRN2IALH4ABwE05EkwkmKIMmLHorfrrZolUbGyqstnexlwuD3NU8t4fXXykVixLoSW7DCy2m5BVKlUhrnqzXaqP6+2RX0s-3RMDIK5+KCUYgaACMonNtZsQUVihTKBYuHw6EQWDSdASYBwQtjAANZGQqIgAGqj4O8pFQX3mYIAXkQ2jmvAA3I5UUA
💻 Code
🙁 Actual behavior
The misleading thing here is that
SubType
plainly cannot be unrelated toSuperType
. It's guaranteed to be a subtype ofSuperType
, since itextends SuperType
.🙂 Expected behavior
The TS 3.5 error message was nearly there (just swapped
{}
forSuperType
)...I might suggest something like this instead though:
This is nearly a garden-variety instance of assigning a wider type to a narrower type, it just looks tricky because of the presence of generics and
extends
.Additional information about the issue
contrast with this example:
(playground)
cc @controversial (discovered in typescript-eslint/typescript-eslint#10461 (comment))
The text was updated successfully, but these errors were encountered: