10000 Bug: [no-unsafe-return] number returned in a callback of `sort` is reported as unsafe any · Issue #5364 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [no-unsafe-return] number returned in a 10000 callback of sort is reported as unsafe any #5364

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

Closed
4 tasks done
hasparus opened this issue Jul 22, 2022 · 7 comments
Closed
4 tasks done
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@hasparus
Copy link
Contributor
hasparus commented Jul 22, 2022

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

https://typescript-eslint.io/play/#ts=4.7.2&jsx=true&sourceType=module&code=MYewdgzgLgBAHhGBeGBtA5AQ3QGhugI3QF0BuAKHIQDoIQAnKACiczwIEpkA+GAb3IwYoSLFABbAA6Z6AUwAmyGJmoAbEMEyrZAYRBSZsppwqCYcqAFd6YYfulz5pGAHoXMAKqRMAM1nnZKxsYEB9lWwADTDAATwiYKBjJBRgANy1LWWoYcgBfDnIgA&eslintrc=N4KABGBEAOCGBOBnApvSAuKABALgT2mUQGN4BLaHAWiIBsyA7HAejiVUgBpwp4BXWkQxhQECJFwEipCtTqMWDAPZU+DRLABmyKvGQ4+8BsMgB3BMZ4BfEFaA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A

Repro Code

const xs = ['a', 'b'];

xs.sort((a, b) => {
  const compared = a.localeCompare(b);

  return compared; // Unsafe return of an `any` typed value. 
})

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unsafe-return": "warn"
  }
};

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

As the tooltip on hover states that compared is a number, and localeCompare returns a number, I expect TypeScript ESLint rules to know it is in fact a number.

Actual Result

no-unsafe-return reports that compared is any.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 5.30.7
@typescript-eslint/parser 5.30.7
TypeScript 4.7.2
ESLint 8.15.0
node web, 16

Edited to include JoshuaKGoldberg's simplified repro.

@hasparus hasparus added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 22, 2022
@hasparus hasparus changed the title Bug: [no-unsafe-return] number returned in a callback of sort Bug: [no-unsafe-return] number returned in a callback of sort is reported as unsafe any Jul 22, 2022
@JoshuaKGoldberg
Copy link
Member
JoshuaKGoldberg commented Jul 22, 2022

Huh, very weird! This also happens if you simplify the code just a bit to always return compared [simplified playground]:

const xs = ['a', 'b'];

xs.sort((a, b) => {
  const compared = a.localeCompare(b);

  return compared; // Unsafe return of an `any` typed value. 
});

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Jul 22, 2022
@hasparus
Copy link
Contributor Author

Weird find, but assigning any of the arguments to an unused local variable cleared the error for me locally.

image

I couldn't reproduce it in the playground, though 🤷‍♂️

@bradzacher
Copy link
Member

This looks like a bug in TS?
Investigating in ts-ast-viewer - it looks like in TS 4.6 a regression was introduced which causes TS to report compared as having an any type.

However i can't reproduce this on the playground... very strange.

@JoshuaKGoldberg JoshuaKGoldberg added triage Waiting for team members to take a look and removed accepting prs Go ahead, send a pull request that resolves this issue labels Jul 25, 2022
@JoshuaKGoldberg
Copy link
Member

Ok, taking a deeper look, this looks to be a similar -or even the same- root issue as #4689 -> microsoft/TypeScript#48313. Marking as external for now.

@JoshuaKGoldberg JoshuaKGoldberg added external This issue is with another package, not typescript-eslint itself and removed triage Waiting for team members to take a look labels Jul 29, 2022
@awesomeunleashed
Copy link

I'm having an issue that's a bit more complex, but might be the same thing as this. Here's my reproduction repo: https://github.com/awesomeunleashed/ts-eslint-unsafe-bug-repro

You can see the error in the README there. In my actual code I started getting random no-unsafe-xxx warnings after some refactoring and they seemed to go away or come back based on seemingly unrelated changes in unrelated files.

If you guys could look if my case looks the same as the TS bug or like I should open a different issue when you have the time, that would be appreciated!

@mcmxcdev
Copy link

Also ran into this with similar (simplified) code:

{data.doctors
  .map((doctor) => doctor.npi)
  .join(', ')}

Hovering over in VSCode shows the correct types though.

@Josh-Cena
Copy link
Member

On the latest TS version, this doesn't seem to be an error anymore.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

6 participants
0