Subwaytime / vite-aliases

Alias auto generation for Vite
https://www.npmjs.com/package/vite-aliases
MIT License
213 stars 13 forks source link

TS type error #36

Closed vsanrocha closed 2 years ago

vsanrocha commented 2 years ago

When I use the plugin in TS file, it's throwing a type error.

Type 'Plugin' is not assignable to type 'PluginOption | PluginOption[]'.
  Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").Plugin' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").Plugin'.
    Types of property 'apply' are incompatible.
      Type '"serve" | "build" | ((config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").ConfigEnv) => boolean)' is not assignable to type '"serve" | "build" | ((config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").ConfigEnv) => boolean)'.
        Type '(config: UserConfig, env: ConfigEnv) => boolean' is not assignable to type '"serve" | "build" | ((config: UserConfig, env: ConfigEnv) => boolean)'.
          Type '(config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").ConfigEnv) => boolean' is not assignable to type '(config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").ConfigEnv) => boolean'.
            Types of parameters 'config' and 'config' are incompatible.
              Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig'.
                Types of property 'plugins' are incompatible.
                  Type '(PluginOption | PluginOption[])[]' is not assignable to type 'PluginOption[]'.
                    Type 'PluginOption | PluginOption[]' is not assignable to type 'PluginOption'.
                      Type 'Plugin' is not assignable to type 'PluginOption'.
                        Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").Plugin' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").Plugin'.
                          Types of property 'apply' are incompatible.
                            Type '"serve" | "build" | ((config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").ConfigEnv) => boolean)' is not assignable to type '"serve" | "build" | ((config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").ConfigEnv) => boolean)'.
                              Type '(config: UserConfig, env: ConfigEnv) => boolean' is not assignable to type '"serve" | "build" | ((config: UserConfig, env: ConfigEnv) => boolean)'.
                                Type '(config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").ConfigEnv) => boolean' is not assignable to type '(config: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig, env: import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").ConfigEnv) => boolean'.
                                  Types of parameters 'config' and 'config' are incompatible.
                                    Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").UserConfig' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").UserConfig'.
                                      Types of property 'plugins' are incompatible.
                                        Type 'PluginOption[]' is not assignable to type '(PluginOption | PluginOption[])[]'.
                                          Type 'PluginOption' is not assignable to type 'PluginOption | PluginOption[]'.
                                            Type 'PluginOption[]' is not assignable to type 'PluginOption | PluginOption[]'.
                                              Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").PluginOption[]' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").PluginOption[]'.
                                                Type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite-aliases/node_modules/vite/dist/node/index").PluginOption' is not assignable to type 'import("/home/vsrocha/projetos/tavola-spa/node_modules/vite/dist/node/index").PluginOption'.
                                                  Type 'Plugin' is not assignable to type 'PluginOption'.ts(2322)

My vite.config.ts file

import { defineConfig } from 'vitest/config';
import { ViteAliases } from 'vite-aliases';

// https://vitejs.dev/config/
export default defineConfig({
  // ...
  plugins: [
    // ...
    ViteAliases({
      useConfig: true,
      useTypescript: true,
      createLog: true,
      logPath: './',
      adjustDuplicates: true
    }),
    // ...
  ],
});
Subwaytime commented 2 years ago

Hmm thats fairly interesting, could you post your tsconfig ?

Subwaytime commented 2 years ago

@vsanrocha could you provide more information on this? - tsconfig would be a great start!

Hellek commented 2 years ago

I have the same issue

{
  "baseUrl": "./src",
  "paths": {
    "@assets/*": ["./assets/*"],
    "@components/*": ["./components/*"],
    "@contexts/*": ["./contexts/*"],
    "@datasource/*": ["./datasource/*"],
    "@pages/*": ["./pages/*"],
    "@router/*": ["./router/*"],
    "@shared/*": ["./shared/*"],
    "@store/*": ["./store/*"],
    "@utils/*": ["./utils/*"]
  },
  "compilerOptions": {
    "target": "ES2020",
    "lib": [
      "dom",
      "dom.iterable",
      "ES2020"
    ],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "ES2020",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "references": [{ "path": "./tsconfig.node.json" }],
  "include": [
    "src"
  ]
}
Subwaytime commented 2 years ago

@Hellek thank you for that! Ill check what the issue is, there wasnt any changes there on my end so it might be a vite change. Are you using vite latest version?

p-gamage commented 2 years ago

I was also getting the same issue and upgrading to the latest vite version seems to have fixed it! Ended upgrading to vitest version to the latest too - 0.12.6

2.9.72.9.9

Subwaytime commented 2 years ago

Already working on a fix, this is due to some changes of the vite plugin api!

Subwaytime commented 2 years ago

Finally had the chance to test against the latest version of vitest and vite. It doesnt seem to be an issue anymore, so the fix is now redundant. If you still encounter this Issue, feel free to post on this Issue again! But please try the latest version of each package beforehand.