core icon indicating copy to clipboard operation
core copied to clipboard

TS error 4058 in generically typed composable containing ref with array of generic type

Open Ragura opened this issue 2 years ago • 5 comments
trafficstars

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

Ragura avatar Dec 05 '22 16:12 Ragura

This seems to be the same report as #7277 just with more info and from another account. Can i close the other one?

LinusBorg avatar Dec 05 '22 19:12 LinusBorg

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!

Ragura avatar Dec 05 '22 19:12 Ragura

It has happened now :)

LinusBorg avatar Dec 05 '22 21:12 LinusBorg

@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 ...

LinusBorg avatar Dec 05 '22 21:12 LinusBorg

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.

pikax avatar Dec 07 '22 08:12 pikax

I'm having a similar problem with generics creating unwanted UnwrapRefs . Here's a reproduction in case it helps.

https://www.typescriptlang.org/play?ssl=46&ssc=50&pln=46&pc=80#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUGUbSpwC+c6UEIcA5GNsAIbUDcAUKJLHDAJ5hFIAqlgDuUBmABKGEmQpVqANwCuqaq1a9+cAIJYeAFQYAjADZEAvIlZxbcAM4xl6dAHVUAEQhYYAYQZQqDrGEMowAFxw0gDG0MgAPAz6ADRwSTwAfMk2dpiopsj2kTFx8Y5Q2ADmqelZrMQaWkSC9qhQALIQaKZwVgg5to7Obp7efgFBIWHFqLFQCek1+nV2ZMD5hZH9q6vAyJHlVdk7dsqtUFgMIKgHMBVY1QMk2Q1NugAKAJIA0qg8nd1etZVkMXO4vD5-IFgqEIlFZqVFmllsdcusCkVgSc4HtbvdHti4GAKooGDBUL8eHijk8Gg1NHwiO0GABrVAA-LMsDxAxwVAAD3JWEK8LmCUOD1SekMJnMGQyQO2dgA2t8cVg4GyeBB0HADABdLZPEH5WZwgyq-XK6h5DHUfVsVbENjOxqMuAc0xcoHMtmernxJW2FptSIhjpdfKOuw6L6UyKxn5-T0ujJsVixLCOOAgVl4AiBIE8vmC1DC+yav46j2Rr0SDIACgA9AAqIkBK4VltNgCUvQV-SemezlTLbTJlhQGGw+cIDeo9koqD21FSQZ2jgnkQbPaNhNWxIgYHsAH5Iu8AjBGKZ4v6JMqDdbWuZojB7XVjXZSBYFQ314TD2PLZiFRE5iB7UCdgYV9gG8TF-2xVoYHwCcG0CexlFMOEL1ga9b1rLkHytBdTVfd8+wQ-duAAC2AewADpAIrKx0Mw+AGArYQxAkaR0HiHCrwYG87zAIin1It99XlZg4CbJs4HiABaRSaLonEKxgaiiEPMxUBAT8wMgwlR2QpwXAAMXkHQ6B3LEqNWTS6PopCUPJP9ZLbexqIgEQqhLK4wHMOAzgYUc4G7EgewMnYGkJWKvx7dNVjktJTB6TSiFBXVgCncxyWQMhoFUisBQC8wNFWQInAuOBRxwcQ3MS+ogA

rizen avatar Apr 06 '23 16:04 rizen

Upgraded to latest vue and is working https://stackblitz.com/edit/vitejs-vite-8vpg3y?file=src%2Fcomposables%2FuseFilter.ts

image

Going to close this ticket, if this still persistent is worth to create a new ticket.

pikax avatar Oct 06 '23 13:10 pikax