Skip to content

Wrong typescript errors when using ref inside ref #5338

Closed
@mseele

Description

@mseele

Steps to reproduce

Run npm run build in codesandbox

What is expected?

No typescript errors

What is actually happening?

Got 2 typescript errors:

src/App.vue:17:3 - error TS2322: Type 'Node[]' is not assignable to type '{ key: number; checked: boolean; }[]'.
  Type 'Node' is not assignable to type '{ key: number; checked: boolean; }'.
    Types of property 'checked' are incompatible.
      Type 'Ref<boolean>' is not assignable to type 'boolean'.

17   nodes.value = array;
     ~~~~~~~~~~~

src/App.vue:32:23 - error TS2345: Argument of type '{ key: number; checked: boolean; }' is not assignable to parameter of type 'Node'.
  Types of property 'checked' are incompatible.
    Type 'boolean' is not assignable to type 'Ref<boolean>'.

32       @input="onInput(toRaw(node))"
                         ~~~~~~~~~~~


Found 2 errors.

See also: vuejs/language-tools#908

Additonal content (SFC where the errors happen)

<script setup lang="ts">
import { toRaw, onMounted, Ref, ref } from 'vue'

interface Node {
  key: number
  checked: Ref<boolean>
}

const nodes = ref<Node[]>([])

onMounted(() => {
  const array: Node[] = new Array<Node>()

  for (const i of [0, 1, 2]) {
    array.push({ key: i, checked: ref(false) })
  }
  nodes.value = array
})

function onInput(node: Node) {
  console.log('onInput', node)
}
</script>

<template>
  <div v-for="node in nodes" :key="node.key">
    <input
      :id="'check-' + node.key"
      :name="'check-' + node.key"
      type="checkbox"
      v-model="node.checked"
      @input="onInput(toRaw(node))"
    />
    <label :for="'check-' + node.key">{{ node.key }}</label>
  </div>
</template>

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