javascript - Importing a JS file in TS with Vite - Stack Overflow

In my initial CRA TS app (Webpack, default setup, etc), I have a JS file that I import into a TS file w

In my initial CRA TS app (Webpack, default setup, etc), I have a JS file that I import into a TS file with no problem.

When I switch to Vite, with vite-plugin-checker, that same import gives me an error:

TS7016: Could not find a declaration file for module '../../utils/hooks/useStateCallback'. '~/dev/admin_vite_worktree/src/app/utils/hooks/useStateCallback.js' implicitly has an 'any' type.

vite.config.ts

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'
import svgrPlugin from 'vite-plugin-svgr';
import macrosPlugin from "vite-plugin-babel-macros"
import checker from 'vite-plugin-checker'
import reactRefresh from '@vitejs/plugin-react-refresh';

// /config/
export default defineConfig({
  // This changes the out put dir from dist to build
  // ment this out if that isn't relevant for your project
  build: {
    outDir: 'build',
  },
  define: {
    global: {},
  },
  plugins: [
    react(),
    reactRefresh(),
    svgrPlugin({
      svgrOptions: {
        icon: true,
      },
    }),
    macrosPlugin(),
    checker({
      typescript: true,
      overlay: {
        panelStyle: 'height: 100vh; max-height: unset;'
      }
    })
  ],
});

tsconfig.json

{
  "pilerOptions": {
    "target": "ESNext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "types": ["vite/client", "vite-plugin-svgr/client"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "checkJs": false
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules",
    "**/*.stories.tsx"
  ]
}

How would I fix this? useStateCallback is actually copied from , with no typings, and I'd rather not convert it to TS if I can avoid it.

(Probably the best answer is if the same useStateCallback functionality is available in a properly typed package somewhere and then I'd just take it out of our codebase haha!)

In my initial CRA TS app (Webpack, default setup, etc), I have a JS file that I import into a TS file with no problem.

When I switch to Vite, with vite-plugin-checker, that same import gives me an error:

TS7016: Could not find a declaration file for module '../../utils/hooks/useStateCallback'. '~/dev/admin_vite_worktree/src/app/utils/hooks/useStateCallback.js' implicitly has an 'any' type.

vite.config.ts

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'
import svgrPlugin from 'vite-plugin-svgr';
import macrosPlugin from "vite-plugin-babel-macros"
import checker from 'vite-plugin-checker'
import reactRefresh from '@vitejs/plugin-react-refresh';

// https://vitejs.dev/config/
export default defineConfig({
  // This changes the out put dir from dist to build
  // ment this out if that isn't relevant for your project
  build: {
    outDir: 'build',
  },
  define: {
    global: {},
  },
  plugins: [
    react(),
    reactRefresh(),
    svgrPlugin({
      svgrOptions: {
        icon: true,
      },
    }),
    macrosPlugin(),
    checker({
      typescript: true,
      overlay: {
        panelStyle: 'height: 100vh; max-height: unset;'
      }
    })
  ],
});

tsconfig.json

{
  "pilerOptions": {
    "target": "ESNext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "types": ["vite/client", "vite-plugin-svgr/client"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "checkJs": false
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules",
    "**/*.stories.tsx"
  ]
}

How would I fix this? useStateCallback is actually copied from https://stackoverflow./a/61842546/6826164, with no typings, and I'd rather not convert it to TS if I can avoid it.

(Probably the best answer is if the same useStateCallback functionality is available in a properly typed package somewhere and then I'd just take it out of our codebase haha!)

Share Improve this question edited Apr 11, 2023 at 11:11 Sultan Aslam 6,2582 gold badges40 silver badges48 bronze badges asked Jul 20, 2022 at 20:25 Jonathan TuzmanJonathan Tuzman 13.3k28 gold badges83 silver badges154 bronze badges 1
  • Did you solve that somehow? I'm struggling with the same issue: reddit./r/sveltejs/ments/17s5y4l/… – Zied Hamdi Commented Nov 10, 2023 at 21:22
Add a ment  | 

1 Answer 1

Reset to default 3

tsconfig.json

"allowJs": false,

change it to

"allowJs": true,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745364051a4624493.html

相关推荐

  • javascript - Importing a JS file in TS with Vite - Stack Overflow

    In my initial CRA TS app (Webpack, default setup, etc), I have a JS file that I import into a TS file w

    17小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信