Importing json file in TypeScript

Sooraj :

I have a JSON file that looks like following:

{

  "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. 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. However I get an error:

Property 'primaryMain' does not exist on type 'typeof "*.json"

Aluan Haddad :

The import form and the module declaration need to agree about the shape of the module, about what it exports.

When you write (a suboptimal practice for importing JSON since TypeScript 2.9 when targeting compatible module formatssee note)

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

You are stating that all modules that have a specifier ending in .json have a single export named default.

There are several ways you can correctly consume such a module including

import a from "a.json";
a.primaryMain

and

import * as a from "a.json";
a.default.primaryMain

and

import {default as a} from "a.json";
a.primaryMain

and

import a = require("a.json");
a.default.primaryMain

The first form is the best and the syntactic sugar it leverages is the very reason JavaScript has default exports.

However I mentioned the other forms to give you a hint about what's going wrong. Pay special attention to the last one. require gives you an object representing the module itself and not its exported bindings.

So why the error? Because you wrote

import a = require("a.json");
a.primaryMain

And yet there is no export named primaryMain declared by your "*.json".

All of this assumes that your module loader is providing the JSON as the default export as suggested by your original declaration.

Note: Since TypeScript 2.9, you can use the --resolveJsonModule compiler flag to have TypeScript analyze imported .json files and provide correct information regarding their shape obviating the need for a wildcard module declaration and validating the presence of the file. This is not supported for certain target module formats.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Importing a JSON file with Typescript

From Dev

Importing JSON file in Node Typescript

From Javascript

Typescript compiler error when importing json file

From Dev

Importing json file in typescript causing "Unexpected token" error with valid json

From Dev

Typescript always errors when importing a config.json file

From Dev

Error "Cannot find module" when importing a remote JSON file in TypeScript

From Dev

importing from a Json file

From Dev

Importing a simple Javascript file in Typescript

From Dev

Importing a JS module in a Typescript file

From Dev

TypeScript Importing JSON, are their any hidden drawbacks to importing JSON this way

From Dev

Remove "default" when importing JSON file in Vite+React using Typescript

From Dev

Importing JSON file into Firebase error

From Dev

Error importing JSON file into mongoDB

From Dev

issue importing json file in r

From Dev

Importing JSON file to access table

From Dev

Importing php json file to swift

From Dev

Importing a typescript file without using any of the exports

From Dev

jest with typescript throws an error when importing a file

From Dev

importing a variable from one typescript file into another

From Dev

Importing a typescript class from another file

From Dev

Importing JSON in TypeScript does not import the contents but a URL

From Dev

Jest/Typescript: Importing a File Breaks Import in Another File

From Dev

PySpark, importing schema through JSON file

From Dev

Importing a JSON file by using module declaration

From Dev

Trouble with importing a file with pandas read_json

From Dev

Importing json file in d3.js

From Dev

angular 7 - importing a json file - compilation error

From Dev

Importing a JSON file into Postgresql 11 on Windows 10

From

Importing json from file into mongodb using mongoimport

Related Related

  1. 1

    Importing a JSON file with Typescript

  2. 2

    Importing JSON file in Node Typescript

  3. 3

    Typescript compiler error when importing json file

  4. 4

    Importing json file in typescript causing "Unexpected token" error with valid json

  5. 5

    Typescript always errors when importing a config.json file

  6. 6

    Error "Cannot find module" when importing a remote JSON file in TypeScript

  7. 7

    importing from a Json file

  8. 8

    Importing a simple Javascript file in Typescript

  9. 9

    Importing a JS module in a Typescript file

  10. 10

    TypeScript Importing JSON, are their any hidden drawbacks to importing JSON this way

  11. 11

    Remove "default" when importing JSON file in Vite+React using Typescript

  12. 12

    Importing JSON file into Firebase error

  13. 13

    Error importing JSON file into mongoDB

  14. 14

    issue importing json file in r

  15. 15

    Importing JSON file to access table

  16. 16

    Importing php json file to swift

  17. 17

    Importing a typescript file without using any of the exports

  18. 18

    jest with typescript throws an error when importing a file

  19. 19

    importing a variable from one typescript file into another

  20. 20

    Importing a typescript class from another file

  21. 21

    Importing JSON in TypeScript does not import the contents but a URL

  22. 22

    Jest/Typescript: Importing a File Breaks Import in Another File

  23. 23

    PySpark, importing schema through JSON file

  24. 24

    Importing a JSON file by using module declaration

  25. 25

    Trouble with importing a file with pandas read_json

  26. 26

    Importing json file in d3.js

  27. 27

    angular 7 - importing a json file - compilation error

  28. 28

    Importing a JSON file into Postgresql 11 on Windows 10

  29. 29

    Importing json from file into mongodb using mongoimport

HotTag

Archive