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

Failed to deduce type for ref on child component #1630

Open
2 tasks done
hongquan opened this issue Sep 20, 2021 · 3 comments
Open
2 tasks done

Failed to deduce type for ref on child component #1630

hongquan opened this issue Sep 20, 2021 · 3 comments

Comments

@hongquan
Copy link

hongquan commented Sep 20, 2021

Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have read the FAQ and my problem is not listed.

Tell us about your environment

  • ESLint version: 7.32.0
  • eslint-plugin-vue version: 7.18.0
  • Node version: v14.17.6
  • Operating System: Ubuntu 21.04

Please show your full configuration:

module.exports = {
  env: {
    browser: true,
    es2021: true,
    'jest/globals': true,
  },
  plugins: [
    'vue',
    '@typescript-eslint',
    'jest',
  ],
  // Ref: https://eslint.vuejs.org/user-guide/#usage
  parser: 'vue-eslint-parser',
  parserOptions: {
    ecmaVersion: 12,
    parser: '@typescript-eslint/parser',
    sourceType: 'module',
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
    extraFileExtensions: ['.vue'],
  },
  extends: [
    // Vue3 has different guide on <template :key>
    'plugin:vue/vue3-essential',
    'standard',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
  ],
  rules: {
    // Without this, eslint misjudge our store mutation definitions
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['error'],
    // We follows braces style in Vue & TypeScript documentation
    'space-before-function-paren': ['error', {
      anonymous: 'always',
      named: 'never',
      asyncArrow: 'always',
    }],
    // Prevent creating change in Git diff
    'comma-dangle': ['error', 'always-multiline'],
    // Allow to skip "await" in front of "router.push".
    '@typescript-eslint/no-floating-promises': ['error', {
      ignoreVoid: true
    }],
    // Our API response follows Python style, camelCase cannot be guaranteed
    'camelcase': 'off',
    'no-void': ['error', { allowAsStatement: true }],
  },
  ignorePatterns: [
    // Config files, don't pass them to @typescript-eslint
    '*.js',
  ]
}

What did you do?

I have a child component in Vue3LottieWeb.vue file, whose setup() function returns play, stop methods.

In parent component, I import that child component and use as template ref:

<script lang="ts">
export default defineComponent({
  setup() {
    const bell = ref<InstanceType<typeof Vue3LottieWeb>>()
    const shakeBell = () => {
      const player = bell.value
      if (player) {
        player.stop()
        player.play()
      }
    }
    return {
      bell,
     shakeBell,
    }
  }
})
</script>

What did you expect to happen?

ESLint should not raise error.

What actually happened?

ESLint complains with "Unsafe assignment of an any value @typescript-eslint/no-unsafe-assignment"

image

When I hover the code in VS Code, Vetur successfully deduces the type of bell and bell.value, but ESLint not.

Repository to reproduce this issue
https://github.com/hongquan/failed-eslint-vue-ref-child-component

@ota-meshi ota-meshi added needs repro Need a repository that can reproduce the problem, or a link to DEMO. needs info labels Sep 20, 2021
@hongquan
Copy link
Author

@ota-meshi Added a reproducible repo.

@ota-meshi
Copy link
Member

I think your problem is the same problem as [no-unsafe-*] rules section in vuejs/vue-eslint-parser#104. Please track of it issue.

@ota-meshi ota-meshi added bug upstream issue and removed needs info needs repro Need a repository that can reproduce the problem, or a link to DEMO. labels Sep 20, 2021
@Maxim-Mazurok
Copy link

I was a bit confused, because I couldn't reproduce no-unsafe-* issues, because imported component seems to have correct type now. However your reproduction still seems to have the same issue:
image

I also found another unrelated issue when comparing your .vue and tsx code:
image

vue-tsc didn't catch type issue with renderer option not matching prop.renderer type.

Anyway, thanks for sharing your repro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants