【问题标题】:Typescript: How to auto generate a d.ts file for image打字稿:如何为图像自动生成 d.ts 文件
【发布时间】:2018-05-14 00:50:15
【问题描述】:

我正在开发一个 React 应用程序并使用 typescript 和 webpack,我知道在 typescript 中如果要导入图像文件,则需要在当前目录中创建一个 d.ts 文件并编写此文件

// index.d.ts
declare module "*.png" {
  const value: string;
  export default value;
}

我的项目目录结构是这样的

-- container
  -- Header
    -- Navigator
         avatar.png
         index.tsx
    -- Banner
         headerGround.png
         index.tsx

如果我要导入png,我需要每个目录写一个d.ts文件,对我来说太繁琐了,有什么办法解决吗?谢谢

【问题讨论】:

    标签: typescript webpack typescript-typings


    【解决方案1】:

    在您的 tsconfig.json 中包含您的 TypeScript 声明文件。

    {
        "compilerOptions" { ... },
        "include": ["path/to/index.d.ts"]
    }
    

    您还可以使用全局模式。例如,typings 目录中可能有多个声明文件。

    "include": ["typings/*.d.ts"]
    

    在此处阅读有关 tsconfig.json 的更多信息:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2016-09-02
      • 2020-04-30
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多