fumeapp / dayjs

Nuxt V3 module for Day.js
Apache License 2.0
111 stars 12 forks source link

Add dayjs-nuxt to nuxt.config.ts modules, then typescript not working #48

Open boss006133 opened 2 months ago

boss006133 commented 2 months ago

reproduction zip: test-typescript.zip

I just create a new nuxt3 app, and add only one module(dayjs-nuxt), then I auto import a const from nuxt.config.ts like the code below

nuxt.config.ts

export default defineNuxtConfig({
  compatibilityDate: "2024-04-03",
  devtools: { enabled: true },
  modules: ["dayjs-nuxt"],
  imports: {
    dirs: ["constants/others"],
  },
  typescript: {
    typeCheck: true,
  },
})

image

and make the typescript not working

image

package.json

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "dayjs-nuxt": "^2.1.9",
    "sass": "1.77.6",
    "sass-loader": "^13.3.2",
    "typescript": "^5.5.4",
    "vue-tsc": "^2.0.29"
  },
  "dependencies": {
    "nuxt": "^3.12.4",
    "nuxt-viewport": "^2.1.5",
    "vue": "latest"
  }
}

If I remove "dayjs-nuxt" from nuxt.config.ts modules, then typescript intellisense works fine

Riadz commented 2 months ago

@boss006133 same issue for me, i just reverted to using dayjs package directly, i created project/utils/dayjs.ts:

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime.js'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import isBetween from 'dayjs/plugin/isBetween'
import duration from 'dayjs/plugin/duration'
import 'dayjs/locale/ar'

dayjs.extend(relativeTime)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(isBetween)
dayjs.extend(duration)

export const $dayjs = dayjs

this will make $dayjs(...) available in both template and script to use, am sure this is not the best way to do it, but until this package is fixed this should do.