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

Vue-tsc Compilation Error for Simple TS File #5091

Open
zhaoyuqiqi opened this issue Dec 27, 2024 · 5 comments
Open

Vue-tsc Compilation Error for Simple TS File #5091

zhaoyuqiqi opened this issue Dec 27, 2024 · 5 comments
Labels
question Further information is requested

Comments

@zhaoyuqiqi
Copy link

Vue - Official extension or vue-tsc version

2.2.0

VSCode version

1.96.0

Vue version

3.5.12

TypeScript version

5.6.2

System Info

No response

package.json dependencies

No response

Steps to reproduce

Declare a function and generate its type using vue-tsc

import { ref } from 'vue'

export function useResetRef<T>(initData: T) {
  const data = ref(initData)
  return data
}

Execute the command vue-tsc-declaration-emitdeclarationonly at the console

npx vue-tsc --declaration --emitDeclarationOnly

What is expected?

Not throw an error and output a type file

What is actually happening?

src/hooks/useResetRef.ts:4:17 - error TS2742: The inferred type of 'useResetRef' cannot be named without a reference to '.pnpm/@VUE[email protected]/node_modules/@vue/shared'. This is likely not portable. A type annotation is necessary.

4 export function useResetRef(initData: T) {
~~~~~~~~~~~

Link to minimal reproduction

https://codesandbox.io/p/devbox/9kmzhk?file=%2Fsrc%2Fhooks%2Fuse-reset.ts%3A5%2C15

Any additional comments?

No response

@RayGuo-ergou
Copy link
Contributor

see #5089 (comment)

I don't have access to edit the codesandbox but in your case this probably will fix

import { ref, type Ref } from 'vue'

export function useResetRef<T>(initData: T) {
  const data: Ref<T> = ref(initData)
  return data
}

@tsukuha
Copy link

tsukuha commented Dec 27, 2024

I could reproduce too error.

Reproduction

package.json

{
  ...(other properties)
  "dependencies": {
    "vue": "^3.5.13",
    "vue-tsc": "^2.2.0"
  }
}

test.vue

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

export function useResetRef<T>(initData: T) {
  const data = ref(initData)
  return data
}

cmd

npx vue-tsc --declaration --emitDeclarationOnly ./test.vue

スクリーンショット 2024-12-27 22 10 23

@RayGuo-ergou
Copy link
Contributor

As mentioned above this is expected and it's how tsc works. ( you can call it limitation of typescript )

@RayGuo-ergou
Copy link
Contributor

In this case specifically:

// 1.
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
export function useResetRef<T>(initData: T) {
  const data: [T] extends [Ref] ? IfAny<T, Ref<T>, T> : Ref<UnwrapRef<T>, UnwrapRef<T> | T> = ref(initData)
  return data
}

// 2. 
export function useResetRef<T>(initData: T) {
  const data: Ref<T | undefined> = ref()
  data.value = initData
  return data
}

// 3.
export function useResetRef<T>(initData: T) {
  const data = ref(initData) as Ref<T>
  return data
}

// 4. Only work prior to vue 3.5 as the reactive system has refactored in 3.5
export function useResetRef<T>(initData: T) {
  const data: Ref<UnwrapRef<T>> = ref(initData) 
  return data
}

@tsukuha
Copy link

tsukuha commented Dec 27, 2024

sorry, I missed read

@KazariEX KazariEX added question Further information is requested and removed pending triage labels Dec 27, 2024
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

4 participants