typescript-eslint: Bug: [no-unsafe-call] Unclear message "Unsafe call of an `error` type typed value" (especially in JSDoc)
The language and UX around no-unsafe-call (and similar rules) messages indicate “an error type typed value”, which seems unclear and maybe also misleading.
Before You File a Bug Report Please Confirm You Have Done The Following…
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
const el = /** @type {Element} */ ({})
el.href.includes('abc')
ESLint Config
module.exports = {
parser: "@typescript-eslint/parser",
rules: {
"@typescript-eslint/<rule-name>": ["error", ...<options>],
},
};
tsconfig
{
"compilerOptions": {
}
}
Expected Result
I expected the error message:
Unsafe call of a method on an `any` typed value
Differences:
- mentions the type
any(which is the only observable type in JSDoc withoutcheckJs: true) - doesn’t mention the type
error - less important: mentions “method”
Actual Result
I observed the error message:
Unsafe call of an `error` type typed value
Confusing parts:
- no mention of
any - mentions the type
error, which is unobservable in JSDoc withoutcheckJs: true- also, if I hear “an
errortype typed value”, then that could sound like something likeError, so even if this “error” language is kept in some form, maybe it should be disambiguated fromError
- also, if I hear “an
- less important: does not mention “method”
Using this in TypeScript or with checkJs: true leads to a more understandable experience:
Hovering over href in el.href shows that TypeScript is reporting an error:
Property 'href' does not exist on type 'Element'.
Additional Info
This also affects other rules, since they also create messages the same way.
The type for this seems to be coming from the ts-api-utils function isIntrinsicErrorType
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 15
- Comments: 16 (6 by maintainers)
In my case, restarting my EsLint server in VsCode fixed the issue
Uh oh! @karlhorky, the image you shared is missing helpful alt text. Check your issue body.
Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.
Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.
same
Uh oh! @KrappRamiro, the image you shared is missing helpful alt text. Check https://github.com/typescript-eslint/typescript-eslint/issues/9591#issuecomment-2259514239.
Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.
Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.
Ha, yeah, I’d meant to file an issue like this once most the PRs moving to “an
errortyped value” were in. This is great. Thanks!For reference:
This rule’s behavior has always been like this (reporting on error-typed values), so I’m not sure why a change in the message motivates a change to its behavior. Nevertheless @kiliancs I would welcome another proposal for your desired change and we can discuss.
The root cause is very often https://github.com/microsoft/vscode-eslint/issues/1774 or something like it. In this issue’s terminology, “error” refers to the inability for TypeScript to create a type for a node. That’s not the same as a type error. We don’t get nice source locations for these, just an “error type”.
In our monorepo, the message for the error type comes up a lot, and developers are often puzzled as to why.
Of course there’s probably always a legit cause, but the cause is often in a far away file that TypeScript or eslint are caching up to. Restarting the eslint server often resolves the issue, while other times we need to perform an intermediate build step. We have a number of intermediate build steps that definitely contribute to this situation (and we are working to address this).
Still, regardless of the specifics of our circumstance, I understand that when the error type occurs, there is a TypeScript error somewhere. Is that right? If so, the additional errors raised by
no-unsafe-<rule>may actually become a distraction from the actual TypeScript error, and the code that is being flagged is often (although not necessarily) just fine.Would typescript-eslint consider adding an option to disable reporting when the issue is an error type? If the response is favorable, I am open to contributing this change.
Happy to create a separate issue for this, but the discussion here seems highly relevant, my point being the confusion may be avoidable, besides message updates.
That motivation sounds pretty good - differentiating between these causes for
anysounds like a helpful thing for users 👍So, understood - an implementation of improving error messages to address this issue should avoid moving back to the original, undifferentiated
anymessage.These messages were recently added to help people differentiate between a real
anyand ananydue to a type that TS can’t resolve (eg due to bad config or something).Often times (not always) an “error” any comes from an out-of-sync workspace (eg you’re using project references and changed another project and ts-eslint hasn’t picked it up). Which is why we tried to differentiate them - so people don’t get as confused about why vscode says the type is there but eslint says it’s any.
More than welcome a PR to improve the message to make that clearer.
I should clarify this - i don’t think that changing the rule logic to disable some reports is an appropriate remedy. We do make an effort to have reasonable squiggly ranges, so there is room to refine the report range to be smaller when it makes sense to do so. Also, it may be reasonable for you to disable this rule entirely in your editor and only run it in CI if that’s a workflow that makes sense for your use case. My point really is that neither of these are solutions that require a change to the rule logic impacting the number of reports that it will be issue - at most the appearance of the reports would be changed.