Skip to content

InstanceType type error:Unsafe assignment of an any value #6882

Closed as not planned
@DearGu

Description

@DearGu

Vue version

3.2.37

Link to minimal reproduction

https://sfc.vuejs.org/#eNqlVM1u00AQfpWpD5BKjVdF4tI6gQohFYle+ClC8qEbe+Jsu961dtcJUcgFkLhyAA48ARckJCSkqjwObbnxCszacf7Kz4GLvTO73zf/Mwn2iiIclhjsBJFNjCgcSK6yThw4Gwdg0ZVFN1YiL7RxMAGDfZhC3+gcrhPs+u787mB8oFMuZ3chm8me3L+KFWPgxgUCGqPNz28fHivL+wjcWpGpHJUD3Qeu4Iir8REMuSwxVolW1kHumbah481H90jDVYKPiCzyjASbGevCc1CllN2W/26SWYDKsC5QQULkGTpwA4REG4MJnQlPvrRam9DpQk9riVxtEk2pUuwLhWmF75WUF0EurjmfY95DAzxJ0FoIK6dBq7Uw4BrM3idcypUwvf10NVjva53KDtRuTXwcdQ5qE7dC/6i1uQvkmy0r67GazuKNWF1JqhsJDvNCcockAUQD5Cma6kySyLP6BMClo6IfkrNSZzoOGn0iqUB0s6q1JiHdwLnC7jCWKF/lYxtqkzGR8wwt8+9DO8wWmJFI3YBQ2zduLpQDFNnAW17SssY9yrqjZN5OpEhO6M08M3HQvXxxev767Mfnl5dvP55/enN59ur72deL96cXX95FrAY2NE1nUvMQSZ1H6u25mVQM52GODC8KNGSg8TDaaLch2kcp9RNtZAq59ePxVJeQihSE24gD1oV2e4FQfDgXGoIHunRo7gt1Ak4TnpGJfZ1jxBY3S6A5+AqO90hD4D3/X0Gv+MCWnbiSSc6JgfP1TDFKRd0nbN4olbgUwqHAURNvxJa6i0TrxhK71H/VP1bBVlBvh3bOi/DYakV7purmeHZBS2an7m+voy7y8qKzbD9Zai06haZUTuQYos3bPaNHFg0Rx8HWEgcj5RBN2yANsfHV/DPn2tMrvJ52SrNFoSxttP9Yl82YC3tHK0dr7+FAj1S921p9Li36vbVYBfMt0FpFzJZNBzZ+p/cc8WyF3X1WaIutSU039Vd/WxF+HIZt4WdllZlaphqepk3+Wf3pL3IGLMY=

Steps to reproduce

// index.vue

<script lang="ts" setup>
import { ref } from 'vue';
import MyModal from './MyModal.vue';

// type error:Unsafe assignment of an `any` value
const modal1 = ref<InstanceType<typeof MyModal> | null>(null); 

// open can get the correct type:(() => boolean) | undefined
// but lint error:Unsafe member access .value on an `any` value & Unsafe call of an `any` typed value
const openModal = () => {
  modal1.value?.open(); // success
};
</script>

<template>
  <header>
    <img
      alt="Vue logo"
      class="logo"
      src="https://cn.vuejs.org/images/logo.svg"
      width="125"
      height="125"
    />
    <button @click="openModal">点击调用子组件方法</button>
    <MyModal ref="modal1" />
    <div class="wrapper">
      <!-- <HelloWorld msg="You did it!"/> -->
      <nav>
        <!-- <RouterLink to="/">Home</RouterLink>
                <RouterLink to="/about">About</RouterLink> -->
      </nav>
      <button>aa</button>
    </div>
  </header>

  <!-- <RouterView/> -->
</template>

<style></style>
// MyModal.vue

<script lang="ts" setup>
import { ref } from 'vue';

const isContentShown = ref(false);
const open = () => (isContentShown.value = !isContentShown.value);

defineExpose({ open });
</script>

<template>
  <div v-if="isContentShown">modal</div>
</template>

<style></style>

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true,
    "baseUrl": ".",
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

tsconfig.node.json

{
  "compilerOptions": {
    "composite": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}

What is expected?

no lint error

What is actually happening?

lint error

System Info

No response

Any additional comments?

Volar ver:1.0.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions