Skip to content

Reactive variable doesn't do type inference #206

@baseballyama

Description

@baseballyama

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I'm using eslint-plugin-svelte. (*.svelte file linting does not work with the parser alone. You should also use eslint-plugin-svelte with it.)
  • I'm sure the problem is a parser problem. (If you are not sure, search for the issue in eslint-plugin-svelte repo and open the issue in eslint-plugin-svelte repo if there is no solution.
  • 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?

8.23.0

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

What did you do?

Configuration
module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  globals: {
    window: true,
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    sourceType: 'module',
    ecmaVersion: 2021,
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.eslint.json'],
    extraFileExtensions: ['.svelte'],
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:svelte/recommended',
  ],
  rules: {},
  overrides: [
    // see: https://github.com/typescript-eslint/typescript-eslint/blob/main/docs/linting/TROUBLESHOOTING.md#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors
    {
      files: ['*.ts'],
      rules: {
        'no-undef': 'off',
      },
    },
    {
      files: ['**/*.svelte'],
      parser: 'svelte-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser',
      },
      rules: {
        'no-undef': 'off',
      },
    },
  ],
};

<script lang="ts">
  let obj = {
    child: {
      title: "hello!",
    },
  };

  $: child = obj.child;
  $: title = child?.title ?? "Yo!";
</script>

{child}{title}

What did you expect to happen?

No ESLint error comes.

What actually happened?

/Users/baseballyama/Desktop/git/issue-eslint-plugin-svelte-1/src/Sample.svelte
  9:6   error  Unsafe assignment of an `any` value            @typescript-eslint/no-unsafe-assignment
  9:14  error  Unsafe member access .title on an `any` value  @typescript-eslint/no-unsafe-member-access

✖ 2 problems (2 errors, 0 warnings)

image

Link to Minimal Reproducible Example

https://github.com/baseballyama/issue-eslint-plugin-svelte-1

Additional comments

I think this is because TypeScript AST is inferred to any.
I'll try to fix this, but before that I would confirm how to fix it.
Do we need to type infer by inserting a dummy let child = obj.child; or something else?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions