device icon indicating copy to clipboard operation
device copied to clipboard

IDE type issues with $device helper

Open gregmulvaney opened this issue 2 years ago • 11 comments

Webstorm is unable to locate the types for the $device helper. Not really familiar with Nuxt plugins/modules so unfortunately I'm not able to locate the issue. App fails nuxi typecheck as well but is able to be built and functions as expected. Tried adding the module to my tsconfig with no fix.

Nuxt: v3.2.2 device: v3.1.0

gregmulvaney avatar Mar 03 '23 18:03 gregmulvaney

~> https://github.com/nuxt/nuxt/issues/19399

danielroe avatar Mar 04 '23 14:03 danielroe

Unless Webstorm is changing something under the hood, I can't see why I would also fail nuxi typecheck with the error of

error TS2339: Property '$device' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<Readonly<ExtractPropTypes<{}>> & VNodeProps & AllowedComponentProps & ComponentCustomProps, never>; ... 10 more ...; $watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends (...args: any) => infer R ? (args_0: R, args_1: R) => ...'.

Implementation

<script setup lang="ts">
const { isMobile } = useDevice()
</script>

<template>
  <div class="min-h-[100dvh] flex flex-col">
    <NavDesktop v-if="!$device.isMobile" />

    <div class="mx-auto px-4 container lg:px-8">
      <slot />
    </div>
    <NavMobile v-if="$device.isMobile" />
  </div>
</template>

gregmulvaney avatar Mar 04 '23 19:03 gregmulvaney

No, that shouldn't happen. Would you provide a reproduction?

danielroe avatar Mar 04 '23 19:03 danielroe

Sure can!

Here is the sandbox: https://codesandbox.io/p/sandbox/great-bash-7zdlg1

Issue persist when running pnpx nuxi typecheck

gregmulvaney avatar Mar 04 '23 20:03 gregmulvaney

That's a bug with this module (or rather, in the build system), as it does not generate types for its plugin: https://unpkg.com/browse/@nuxtjs/[email protected]/dist/runtime/plugin.d.ts.

danielroe avatar Mar 06 '23 11:03 danielroe

Yeah, I get a similar error in VSCode: '__VLS_ctx.$device' is of type 'unknown'.

nathanchase avatar Mar 19 '23 18:03 nathanchase

yep, same error on vs code:

image

joseehilton147 avatar Mar 21 '23 14:03 joseehilton147

I came across the same issue, I shimmed it by doing:

import { ComponentCustomProperties } from 'vue';

type Device = {
  userAgent: string;
  isDesktop: boolean;
  isIos: boolean;
  isAndroid: boolean;
  isMobile: boolean;
  isMobileOrTablet: boolean;
  isDesktopOrTablet: boolean;
  isTablet: boolean;
  isWindows: boolean;
  isMacOS: boolean;
  isApple: boolean;
  isSafari: boolean;
  isFirefox: boolean;
  isEdge: boolean;
  isChrome: boolean;
  isSamsung: boolean;
  isCrawler: boolean;
};

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $device: Device;
  }
}

max-arias avatar Apr 21 '23 21:04 max-arias

I get a similar error in VSCode:

'__VLS_ctx.$device' is of type 'unknown'.

popdo avatar Sep 24 '23 09:09 popdo

I solved the problem for myself like this

// plugins.d.ts

import type { Device } from "@nuxtjs/device/runtime/types";

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $device: Device;
  }
}

sholohov avatar Feb 18 '24 11:02 sholohov

I solved the problem for myself like this

// plugins.d.ts

import type { Device } from "@nuxtjs/device/runtime/types";

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $device: Device;
  }
}

That helped, but would be nice to have it out of the box in library

steklopod avatar Feb 18 '24 13:02 steklopod