Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Vue 3 + TS event types error #29

Closed
igbominadeveloper opened this issue May 10, 2021 · 1 comment
Closed

Vue 3 + TS event types error #29

igbominadeveloper opened this issue May 10, 2021 · 1 comment
Labels
question Further information is requested

Comments

@igbominadeveloper
Copy link

I created a reusable component with this template

<template>
  <label :for="label" class="mb-1 text-sm flex items-center">
    <input
      type="checkbox"
      :id="label"
      :checked="modelValue"
      class="mr-1 rounded-sm"
      @change="$emit('update:modelValue', $event.targe.checked)"
      v-bind="$attrs"
    />
    {{ label }}
  </label>

  <p>{{ error }}</p>
</template>

When I run vue-tsc, I get these errors:

src/components/BaseCheckbox.vue:8:43 - error TS2531: Object is possibly 'null'.

@change="$emit('update:modelValue', $event.target.checked)"


src/components/BaseCheckbox.vue:8:57 - error TS2339: Property 'checked' does not exist on type 'EventTarget'.

@change="$emit('update:modelValue', $event.target.checked)"
                                                         ~~~~~~~

To be honest, I am not sure this is a vue-tsc problem, I just don't know where to turn to. Any help is welcome

@igbominadeveloper igbominadeveloper changed the title Property 'value' does not ex Vue 3 + TS event types May 10, 2021
@igbominadeveloper igbominadeveloper changed the title Vue 3 + TS event types Vue 3 + TS event types error May 10, 2021
@johnsoncodehk
Copy link
Owner

johnsoncodehk commented May 11, 2021

When vue support typescript in template(vuejs/core#1359), you can fix it by this ($event.target as any).checked.

But for now you need to use a helper function:

<template>
...
@change="emitModelValue"
...
</template>

<script lang="ts">
...
function emitModelValue(event: Event) {
  emit('update:modelVolar', (event.target as any).checked);
}
...
</script>

You can ask TS question in Vue Discord next time, you will have more immediate response.

@johnsoncodehk johnsoncodehk added the question Further information is requested label May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants