【发布时间】:2022-01-02 09:33:08
【问题描述】:
除了其他声明之外,我还在我的 ./src/types.d.ts 文件中声明了以下模块:
/// <reference types="@emotion/react/types/css-prop" />
import '@emotion/react';
import { PureComponent, SVGProps } from 'react';
declare module '*.svg' {
export class ReactComponent extends PureComponent<SVGProps<SVGSVGElement>> {}
}
declare module '*.ttf';
declare module '*.eot';
declare module '*.woff';
declare module '*.woff2';
当我运行tsc --noEmit 时出现类似...的错误
src/fonts/fonts.ts:39:31 - error TS2307: Cannot find module './pt-sans-v12-latin-ext_latin-regular.woff' or its corresponding type declarations.
39 import PTSansregularWOFF from './pt-sans-v12-latin-ext_latin-regular.woff';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/fonts/fonts.ts:40:32 - error TS2307: Cannot find module './pt-sans-v12-latin-ext_latin-regular.woff2' or its corresponding type declarations.
40 import PTSansregularWOFF2 from './pt-sans-v12-latin-ext_latin-regular.woff2';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/layouts/default/NavigationBar.tsx:4:41 - error TS2307: Cannot find module '../../images/brand.svg' or its corresponding type declarations.
4 import { ReactComponent as Brand } from '../../images/brand.svg';
~~~~~~~~~~~~~~~~~~~~~~~~
...被吐出来了。
我的tsconfig.json 看起来像这样:
{
"include": ["./src/**/*", "./.gatsby/**/*", "./*.ts", "./plugins/**/*"],
"files": ["./src/types.d.ts"],
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": ["dom", "ES2017", "ES2019"],
"jsx": "react-jsx",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmit": true,
"skipLibCheck": true,
"moduleResolution": "node",
"resolveJsonModule": true
}
}
如何让 Typescript 找到我对应的类型声明?
有趣的是我的types.d.ts 的其他部分有效果,比如我定义@emotion/react 的接口Theme 时。
【问题讨论】:
标签: typescript tsconfig