TS error 4058 in generically typed composable containing ref with array of generic type
Vue version
3.2.45
Link to minimal reproduction
https://stackblitz.com/edit/vitejs-vite-eoqyep?file=src/composables/useFilter.ts
Steps to reproduce
The TS error presents itself inside the useFilter.ts
composable inside the composables
folder.
What is expected?
I'm trying to create a composable with a generic type T
, which inside the function creates a ref()
containing an array of objects of the generic type T
.
This is the composable:
export function useFilter<T>() {
const data = ref<T[]>([]);
return { data };
}
What is actually happening?
It throws the following TS error, as seen in the reproduction link:
Return type of exported function has or is using name 'RawSymbol' from external module "stackblitz:/node_modules/@vue/reactivity/dist/reactivity" but cannot be named.(4058)
After a discussion on Discord, we found that the following rewrite of the composable fixes the issue, but in a very verbose and unwanted way:
export function useFilter<T>(): {
data: Ref<UnwrapRef<T[]>>;
} {
const data = ref<T[]>([]);
return { data };
}
Needless to say this would get very annoying when the composable has many more return properties.
Perhaps important to debug this issue: removing declaration: true
from the tsconfig.json
file also gets rid of the TS error, but as this composable is part of a library that's not an option for me.
Removing the default empty array value for the ref prevents the issue as well.
System Info
System:
OS: macOS 13.0
CPU: (10) arm64 Apple M1 Pro
Memory: 77.06 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 17.0.1 - ~/.nvm/versions/node/v17.0.1/bin/node
npm: 8.13.1 - ~/.nvm/versions/node/v17.0.1/bin/npm
Browsers:
Chrome: 107.0.5304.121
Safari: 16.1
Any additional comments?
No response
This seems to be the same report as #7277 just with more info and from another account. Can i close the other one?
This seems to be the same report as #7277 just with more info and from another account. Can i close the other one?
Yes, my apologies, that was a colleague's quick report of the same issue. I was told it would be deleted when I made a more thorough one instead, but since that hasn't happened yet you can safely close the other issue!
@pikax It feels we had this or a very similar issue before ... can you recall?
Should we just re-export that type? Is that really necessary? Those are private so far for a reason, and have worked for many lib authors ...
This is odd error, because those types are internals, only Ref
should be exposed on the types declaration file, the RawSymbol
should be only used in the library itself.
Upgraded to latest vue and is working https://stackblitz.com/edit/vitejs-vite-8vpg3y?file=src%2Fcomposables%2FuseFilter.ts
Going to close this ticket, if this still persistent is worth to create a new ticket.