Typescript compiler error when importing json file

后端 未结 6 1646
别跟我提以往
别跟我提以往 2020-12-01 15:50

So the code is simple:

calls.json

{\"SERVER\":{
    \"requests\":{
      \"one\":\"1\"
    }
} }

file.ts

import jso         


        
6条回答
  •  执念已碎
    2020-12-01 16:09

    As of Typescript 2.9 you can import JSON file natively without any additional hack/loader needed.

    The following excerpt is copied from said link above.

    ...TypeScript is now able to import JSON files as input files when using the node strategy for moduleResolution. This means you can use json files as part of their project, and they’ll be well-typed!

    ./src/settings.json

    {
        "dry": false,
        "debug": 
    

    ./src/foo.ts

    import settings from "./settings.json";
    
    settings.debug === true;  // Okay
    settings.dry === 2;       // Error! Can't compare a `boolean` and `number`
    

提交回复
热议问题