vueuse: Typescript Error when using useStorage in Pinia Store - pnpm

Describe the bug

When using useStorage in a Pinia store declaration typescript throws the following error:

The inferred type of 'useCounterStore' cannot be named without a reference to '.pnpm/@vueuse+shared@9.8.0_vue@3.2.47/node_modules/@vueuse/shared'. This is likely not portable. A type annotation is necessary.

2023-02-25 11_33_07-counter ts

The Error disappears when importing RemovableRef seperately but then there would be an unused import. I also found that the error disappears when setting "preserveSymlinks: true" in the tsconfig but then typescript complains about vue not having exports named Ref, createApp etc.

I could reproduce it by setting up a new project with create-vue and just installing vueuse/core, therefore I believe it actually is a bug, but please correct me if some setup with pnpm/typescript etc. is wrong here.

Reproduction

https://github.com/robinpilz/vueuse-pinia-minimal-repro

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 4.66 GB / 15.47 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.1.0 - ~/.nvm/versions/node/v18.1.0/bin/node
    Yarn: 1.22.18 - ~/.yarn/bin/yarn
    npm: 8.18.0 - ~/.nvm/versions/node/v18.1.0/bin/npm
  Browsers:
    Firefox: 110.0
  npmPackages:
    @vueuse/core: 9.8.0 => 9.8.0 
    vue: ^3.2.47 => 3.2.47

Used Package Manager

pnpm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15

Most upvoted comments

I found a quickfix, which is to import type RemovableRef. You don’t even have to use it anywhere, just importing it will fix the TS error.

Example below taken from your repro, but turned into Setup store notation, haven’t tested Option syntax).

Before:

import { useLocalStorage } from "@vueuse/core";
import { defineStore } from "pinia";

export const useCounterStore = defineStore('counterStore', () => {
  const test = useLocalStorage('test', false);

  return {
    test,
  };
});

After:

import { useLocalStorage } from "@vueuse/core";
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- Needed to prevent TS error
import { defineStore, type RemovableRef } from "pinia";

export const useCounterStore = defineStore('counterStore', () => {
  const test = useLocalStorage('test', false);

  return {
    test,
  };
});

After (variant)

To make sure other devs don’t remove the import, you could use it like this:

import { useLocalStorage } from "@vueuse/core";
import { defineStore, type RemovableRef } from "pinia";

export const useCounterStore = defineStore('counterStore', () => {
  const test = useLocalStorage('test', false) satisfies RemovableRef<boolean>;

  return {
    test,
  };
});

Note that I’d use satisfies, not as, since we preferably don’t do any type casting, we just want to validate that it’s the given type.

Small correction to the above workaround: type RemovableRef is exported by @vueuse/core, not pinia 👍

import { type RemovableRef } from '@vueuse/core`

Shouldn’t this issues be re-opened ? The hot fix seems to work, but not sure this is the intended behavior