Typescript compiler “cannot find module” when using Webpack require for CSS/image assets

后端 未结 1 1606
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 00:31

I am writing vanilla Javascript but using the Typescript compiler\'s checkJs option to do type checking in VSCode. I have Webpack set up to load v

1条回答
  •  遥遥无期
    2021-02-14 01:28

    Requiring non JS or TS resources is currently not supported by the TypeScript server which powers VS Code's JavaScript and TypeScript intellisense. Here's the issue tracking this: https://github.com/Microsoft/TypeScript/issues/15146

    As a workaround, try creating a d.ts file in your project with the content:

    declare module '*.css' { export default '' as string; }
    declare module '*.png' { export default '' as string; }
    

    You can also suppress individual errors by adding // @ts-ignore before the require:

    // @ts-ignore
    var img = require("../img/image.png");
    

    0 讨论(0)
提交回复
热议问题