Skip to content

Bug: ref typings should be inferred from element tag #342

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

Closed
shigma opened this issue Aug 3, 2021 · 3 comments
Closed

Bug: ref typings should be inferred from element tag #342

shigma opened this issue Aug 3, 2021 · 3 comments
Labels
question Further information is requested

Comments

@shigma
Copy link

shigma commented Aug 3, 2021

<template>
    <ul :ref="el => state.element = el"></ul>
</template>

<script lang="ts" setup>
    import { ref } from 'vue'
    const state = ref<{ element: Element }>({ element: null })
</script>

This will produce error because ComponentInternalInstance | Element cannot be assigned to Element.

However we can infer the real :ref is string | Ref<any> | ((ref: HTMLUListElement) => void) instead of string | Ref<any> | ((ref: ComponentInternalInstance | Element) => void) because ul is hard-coded.


Or, can we prevent the error message by <!-- @ts-ignore --> or something?

@shigma shigma changed the title Feature: Infer :ref typings by element Feature: Infer ref typings by element Aug 3, 2021
@johnsoncodehk
Copy link
Member

johnsoncodehk commented Aug 3, 2021

You should define correct type param to ref, and make sure your vue version >= 3.2, because vue 3.1.5 has a type bug in this case.

<template>
  <ul :ref="el => state.element = el"></ul>
</template>

<script lang="ts" setup>
import { ref, ComponentInternalInstance } from 'vue'
const state = ref<{ element: ComponentInternalInstance | Element | null }>({ element: null })
</script>

Btw @ts-ignore already available:

<template>
  <ul :ref="el => {
    // @ts-ignore
    state.element = el
  }"></ul>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
// @ts-ignore
const state = ref<{ element: Element }>({ element: null })
</script>

@johnsoncodehk johnsoncodehk added the question Further information is requested label Aug 3, 2021
@shigma
Copy link
Author

shigma commented Aug 3, 2021

You should define correct type param to ref, and make sure your vue version >= 3.2, because vue 3.1.5 has a type bug in this case.

The problem is, the CORRECT type param to ref is ToRef<Element>. If I use something else just to please volar, I will have to add a thousand more type conversions to rest of my code.

@shigma shigma changed the title Feature: Infer ref typings by element Bug: ref typings should be inferred from element tag Aug 3, 2021
@johnsoncodehk
Copy link
Member

johnsoncodehk commented Aug 3, 2021

If I use something else just to please volar, I will have to add a thousand more type conversions to rest of my code.

Please don't say that, you are not to please volar, you to please typescript. You can reproduce same errors in the *.ts.

// foo.tsx
import { ref } from 'vue'

const state = ref<{ element: Element }>({ element: null })
const _ul = <ul ref={el => state.element = el}></ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants