typescript-eslint: Bug: [no-unsafe-member-access] JSDoc type assertion not applied in chained expression

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=5.4.3&fileType=.js&code=MYewdgzgLgBAhjAvDA9AKjTAAlAngBwFMYBvOMXAXxjRRgAoTKBKAKFfUxwOJJJlBgAZgEsA5gC4GzJAD4YAVzABrMCADuYGJWq0GrGPFbMAdINFj6zANztB0GCLBRCAJwC2hACYi4LpKgY2HhEpPzm4lJWcooqapraunT0cDasTi4e3r4uZuAWVrasQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tieQEMAZoloBbRCIBGiaLT5kyKVBkjToHaJHBgAviG1A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDRh9BAKWFhR6KQF8QqoA&tokens=false

Repro Code

index.js

const a = /** @type {any} */ ({})

/** @type {{ config: () => unknown }} */ (
  a
).config();

const intermediate = /** @type {{ config: () => unknown }} */ (a);
intermediate.config();

error on  in chained expression showing "Unsafe member access .config on an  value. ESLint @typescript-eslint/no-unsafe-member-access"

no error on the  in

TypeScript version

The TypeScript equivalent of the chained expression with the type assertion has no problems:

({} as { config: () => unknown }).config();

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unsafe-member-access": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true,
    "checkJs": true
  }
}

Expected Result

The JSDoc type assertion on the chained expression should be treated the same way (no error) as the JSDoc type assertion on the intermediate variable creation

Actual Result

The JSDoc type assertion on the chained expression results in an error on .config() whereas the JSDoc type assertion on the intermediate variable creation does not result in an error

Error message:

Unsafe member access .config on an `any` value. eslint (@typescript-eslint/no-unsafe-member-access)

Additional Info

–

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (13 by maintainers)

Most upvoted comments

I’m closing it because it will be automatically fixed by #1682, and we likely don’t want to specifically make a workaround in this particular rule. Nevertheless if anyone has nice&easy tricks to make TS tell us the asserted type I’m all ears.

nice, cool trick with the comma expression 👍

I’ll stick with the intermediate variable workaround for now:

const intermediate = /** @type {{ config: () => unknown }} */ (a);
intermediate.config();

Let’s investigate, but I doubt we are doing anything obviously wrong.

Yeah so the issue here is that JSDoc type assertions are not part of the AST - they’re just comments in the code. So the lint rules can’t see them when they traverse the AST.

When a lint rule asks for the type of the “object” in the member expr it only sees the identifier - which is typed as any.

The correct fix, as Josh mentioned, is adding AST support for a JSDoc assertion. The difficult thing here is that for TS the “assertion node” doesn’t include the comment - it’s just the parentheses!

The reason the sequence expression works is because it changes the “object” from the identifier (which is typed as any) to the sequence expression, which encapsulates the assertion. It’s essentially a short-hand for the separate variable approach.

(not actually helpful advice)

You can always work around this with

const a = /** @type {any} */ ({});

(0, /** @type {{ config: () => unknown }} */ (
  a
)).config();

😁

All good, totally understand that there’s a lot of noise in open source issues! Really appreciate the work that the team does.

@karlhorky Sorry for the hangup on the ASI red herring!

My current guess is we are not tickling the compiler in the right way, so it does not fully use JSDoc in JS files… not sure.

Oh, hmmmmmmm.

to your point,

const a = /** @type {any} */ ({});

// correctly has type string
const s = (/** @type {{ config: () => string }} */ (
  a
).config());

resolving it doesn’t resolve the JSDoc typing problem. As @Josh-Cena mentioned, the rule just uses the types as provided by TS

Understood. So this is actually a bug in TypeScript’s JSDoc type assertion support.

Yeah, I looked further into this, and, while you definitely have an ASI bug, which will impact control flow


const a = /** @type {any} */ ({}); // <-- semicolon needed here

/** @type {{ config: () => unknown }} */ (
  a
).config();

resolving it doesn’t resolve the JSDoc typing problem. As @Josh-Cena mentioned, the rule just uses the types as provided by TS. The critical point is when I hover over a in the playground, I still see any. If the type said { config: () => unknown } (as with the intermediate variable), and the linter still flagged the code, then we could consider this a bug on our part 🙂.

I suggest asking for help on the TS Discord or filing an issue with TS on how to get the JSDoc types to work correctly.

This rule does not care if you are using TypeScript or JSDoc. We ask TypeScript to give us the type of something, and it seems you failed to provide the right types for your code, probably due to ASI as @kirkwaiblinger said. In any case there’s nothing we should do.

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.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.