在 TypeScript 中導入 JSON 文件 - Importing JSON file in TypeScript

問題:

I have a JSON file that looks like following:我有一個如下所示的JSON文件:

{

  "primaryBright":    "#2DC6FB",
  "primaryMain":      "#05B4F0",
  "primaryDarker":    "#04A1D7",
  "primaryDarkest":   "#048FBE",

  "secondaryBright":  "#4CD2C0",
  "secondaryMain":    "#00BFA5",
  "secondaryDarker":  "#009884",
  "secondaryDarkest": "#007F6E",

  "tertiaryMain":     "#FA555A",
  "tertiaryDarker":   "#F93C42",
  "tertiaryDarkest":  "#F9232A",

  "darkGrey":         "#333333",
  "lightGrey":        "#777777"
}

I'm trying to import it into a .tsx file.我正在嘗試將其導入.tsx文件。 For this I added this to the type definition:爲此,我將其添加到類型定義中:

declare module "*.json" {
  const value: any;
  export default value;
}

And I'm importing it like this.我是這樣導入的。

import colors = require('../colors.json')

And in the file, I use the color primaryMain as colors.primaryMain .在文件中,我使用顏色primaryMain作爲colors.primaryMain However I get an error:但是我收到一個錯誤:

Property 'primaryMain' does not exist on type 'typeof "*.json"屬性 'primaryMain' 在類型 'typeof "*.json" 上不存在


解決方案:

參考一: https://en.stackoom.com/question/3NmMS
參考二: https://stackoom.com/question/3NmMS
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章