Skip to content

false positive no-unsafe-member-access when accessing same class fields inside $derived #1274

@Theo-Steiner

Description

@Theo-Steiner

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.

What version of ESLint are you using?

@eslint/js@9.31.0

What version of eslint-plugin-svelte are you using?

eslint-plugin-svelte@3.11.0

What did you do?

Configuration
import js from "@eslint/js";
import sveltePlugin from "eslint-plugin-svelte";
import tseslint from "typescript-eslint";
export default tseslint.config(
  js.configs.recommended,
  ...tseslint.configs.recommended,
  ...sveltePlugin.configs["flat/recommended"],
  {
    files: ['**/*.svelte.ts'],
    languageOptions: {
      parserOptions: {
        parser: tseslint.parser,
        extraFileExtensions: [".svelte"],
        projectService: true
      },
    }
  },
  {
  	rules: {
			'@typescript-eslint/no-unsafe-member-access': 'error',
		}
  }
);
export class Product {
  x = $state(1)
  y = $state(2)
  // broken
  result = $derived(this.x * this.y) // Unsafe member access .x on an `any` value. [@typescript-eslint/no-unsafe-member-access], Unsafe member access .y on an `any` value. [@typescript-eslint/no-unsafe-member-access]
}

What did you expect to happen?

accessing fields of the same class (this.x or this.y) from a call to $derived should infer the correct this of the surrounding class.

What actually happened?

a false positive no-unsafe-member-access error is triggered as this is inferred as any inside $derived

Link to GitHub Repo with Minimal Reproducible Example

eslint playground

Additional comments

Workaround

the error can be dodged by using $derived.by since the this type is inferred correctly inside the callback function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions