Skip to content
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

Bug: [no-misused-promises] Cannot read properties of undefined (reading 'flags') #5807

Closed
4 tasks done
jlowcs opened this issue Oct 11, 2022 · 4 comments · Fixed by #5809
Closed
4 tasks done

Bug: [no-misused-promises] Cannot read properties of undefined (reading 'flags') #5807

jlowcs opened this issue Oct 11, 2022 · 4 comments · Fixed by #5809
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jlowcs
Copy link

jlowcs commented Oct 11, 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.8.4&sourceType=module&code=GYVwdgxgLglg9mABAcwKZQJJjKgTgNQEMAbEVACgDprDdkBnALkV1UIBMFiBPRAbQC6ASmbh2qYDBzsA3AChQkWAhTosOAiTJUadJizacwPfvSi4pyYaLDjJ0+YujwkaTNjxFSFapQD6tAzMrBxcvGYWYMiCIohiElKo7IgA3nKIBlAguEjx9knyAL5ycqgAHgAOcLhQiE7Kruhe2rF5iclpGaxZOaruGs0UAOSEQ0JFQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oFtLlZkiACa1i0Dr0GoMAbXDYciaOOiQANPIVYF2SGQAWiMgGtkANQ6UhAJUT5Y0JukyadOAIb58VAEax8KM4AZu7wgq4KAL4RYNEKALry0ZFAA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

function getInnerValue(...args: readonly []): undefined;
function getInnerValue(...args: readonly [string]): undefined;
function getInnerValue(..._args: readonly string[]): undefined {
  return undefined;
}

export function getValue(): undefined {
  return getInnerValue('a');
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-misused-promises": [
      "error",
      {
        "checksVoidReturn": {
          "attributes": false
        }
      }
    ]
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

eslint runs without error

Actual Result

TypeError: Cannot read properties of undefined (reading 'flags')
Occurred while linting <input>:8
Rule: "@typescript-eslint/no-misused-promises"
    at SU.isUnionType (https://typescript-eslint.io/sandbox/index.js:20:512744)
    at Object.c [as unionTypeParts] (https://typescript-eslint.io/sandbox/index.js:20:515201)
    at https://typescript-eslint.io/sandbox/index.js:20:893906
    at m2 (https://typescript-eslint.io/sandbox/index.js:20:893955)
    at https://typescript-eslint.io/sandbox/index.js:20:900093
    at d (https://typescript-eslint.io/sandbox/index.js:20:900175)
    at https://typescript-eslint.io/sandbox/index.js:20:258820
    at https://typescript-eslint.io/sandbox/index.js:20:249337
    at Array.forEach (<anonymous>)
    at Object.emit (https://typescript-eslint.io/sandbox/index.js:20:249325)

Additional Info

The issue seems to be coming from the function being defined in a polymorphism manner with rest args.

For instance, this produces the error:

function getInnerValue(...args: readonly []): undefined;
function getInnerValue(...args: readonly [string]): undefined;
function getInnerValue(..._args: readonly string[]): undefined {
	return undefined;
}

export function getValue(): undefined {
	return getInnerValue('a');
}

But this does not:

function getInnerValue(args: readonly []): undefined;
function getInnerValue(args: readonly [string]): undefined;
function getInnerValue(_args: readonly string[]): undefined {
	return undefined;
}

export function getValue(): undefined {
	return getInnerValue(['a']);
}

And neither does this:

function getInnerValue(...args: readonly string[]): undefined {
	return undefined;
}

export function getValue(): undefined {
	return getInnerValue('a');
}

Versions

package version
@typescript-eslint/eslint-plugin 5.40.0
@typescript-eslint/parser 5.40.0
TypeScript 4.8.4
ESLint 8.15.0
node 18.9.1
@jlowcs jlowcs added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Oct 11, 2022
@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Oct 11, 2022
@Aaron1011
Copy link
Contributor

Shorter repro:

function getInnerValue(...args: []): void;

export function getValue(): undefined {
  return getInnerValue('a');
}

I didn't know that ...args: [] was valid TypeScript. It should hopefully be a simple fix to check for it, and avoid calling Object.unionTypeParts

@mrlonis
Copy link

mrlonis commented Oct 11, 2022

This is affecting me in various projects and almost always occurs with @typescript-eslint/no-misused-promises

Aaron1011 added a commit to Aaron1011/typescript-eslint that referenced this issue Oct 11, 2022
…sused-promises

Fixes typescript-eslint#5807

When linting a function with a 'rest' tuple argument, we might
have different numbers of arguments between signatures:

```
function foo(...args: []);
function foo(...args: [string]);
```

This PR makes no-misused-promises handle this code correctly,
by ensuring that we only check an argument when we have a corresponding
type parameter in the tuple type arguments.
JoshuaKGoldberg pushed a commit that referenced this issue Oct 11, 2022
…sused-promises (#5809)

Fixes #5807

When linting a function with a 'rest' tuple argument, we might
have different numbers of arguments between signatures:

```
function foo(...args: []);
function foo(...args: [string]);
```

This PR makes no-misused-promises handle this code correctly,
by ensuring that we only check an argument when we have a corresponding
type parameter in the tuple type arguments.
@JoshuaKGoldberg JoshuaKGoldberg changed the title Bug: [no-misued-promises] Cannot read properties of undefined (reading 'flags') Bug: [no-misused-promises] Cannot read properties of undefined (reading 'flags') Oct 12, 2022
@alfaproject
Copy link

Just also got hurt by this. @JoshuaKGoldberg I guess you'll be preparing a release soon?

@bradzacher
Copy link
Member

bradzacher commented Oct 13, 2022

We have a standard release schedule:
https://github.com/typescript-eslint/typescript-eslint#versioning

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
6 participants