facebook / facebook/docusaurus

TypeScript emits type errors “Cannot find modules '<path of raster images>' when raster images are imported

未关闭
#8,683 5 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
TypeScript
星标
66.2k
派生
10k
平均合并
1 天 3 小时
30 天内合并 PR
52

描述

### Have you read the Contributing Guidelines on issues?

- [X] I have read the [Contributing Guidelines on issues](https://github.com/facebook/docusaurus/blob/main/CONTRIBUTING.md#issues).

### Prerequisites

- [X] I'm using the latest version of Docusaurus.
- [X] I have tried the `npm run clear` or `yarn clear` command.
- [X] I have tried `rm -rf node_modules yarn.lock package-lock.json` and re-installing packages.
- [X] I have tried creating a repro with https://new.docusaurus.io.
- [X] I have read the console error message carefully (if applicable).

### Description

In current Docusaurus version (2.x), no `declare module "*.png"`, `declare module "*.jpg"`, and so on are not defined and we get type erros when we try to import raster images unlike Next.js.
We have to avoid this kind of errors by adding these ambient declarations to `src/index.d.ts`.

To make matters worse, the `plugin-ideal-image` changes the type of imported supported images from `string` to `{src: {src: string, width: ...}, preSrc: string}`. (related: #8684) We will have to select proper ambient declarations. Next.js, which has a similar feature, resolves this problem by `next-env.d.ts` like:

```ts
///
///

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
```

### Reproducible demo

https://codesandbox.io/s/docusaurus-bug-report1-kptbhq

### Steps to reproduce

1. Setup using TypeScript template
2. Add `import picture from "../../blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg";` and `` to `src/pages/index.tsx`

### Expected behavior

No type errors

### Actual behavior

```
Cannot find module '../../blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg' or its corresponding type declarations.ts(2307)
```

![image](https://user-images.githubusercontent.com/12870451/219943719-5ccee68d-90ee-4ef0-b677-fb241f855753.png)

### Your environment

- Public source code: https://codesandbox.io/s/docusaurus-bug-report1-kptbhq
- Docusaurus version used: 2.3.1
- Environment name and version (e.g. Chrome 89, Node.js 16.4): CodeSandbox (Node 16) + TypeScript 4.9.5
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): CodeSandbox

### Self-service

- [ ] I'd be willing to fix this bug myself.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。