简体   繁体   English

在 TypeScript 中将 JSON 文件导入为 const

[英]Import JSON file as const in TypeScript

I would like to create a union type from a property of the elements of an array.我想从数组元素的属性创建联合类型。 If the array were inline, that would be pretty straightforward using a constant assertion.如果数组是内联的,那么使用常量断言将非常简单。

const arr = [{ "name": "One" }, { "name": "Two" }] as const;

type name = typeof arr[number]["name"];
// name = "One" | "Two"

Note that without as const the type name becomes equal to string , which is not the intent.请注意,如果没有as const ,类型name将等于string ,这不是意图。

The problem I'm facing is that the array is defined in a separate JSON file.我面临的问题是数组是在单独的 JSON 文件中定义的。 I set "resolveJsonModule": true in the TypeScript options so I can import the JSON file in my module.我在 TypeScript 选项中设置了"resolveJsonModule": true以便我可以在我的模块中导入 JSON 文件。 But then the compiler widens the type of all properties in the array as there is no as const on the definition.但是随后编译器扩展了数组中所有属性的类型, as const定义中没有as const

import * as arr from "./array.json";

type name = typeof arr[number]["name"];
// name = string (!)

Is there a way I can import a JSON file without type widening?有没有办法可以在不扩展类型的情况下导入 JSON 文件?

I needed an answer to this too, and then realized, "Hey, that wouldn't be a hard npm package to write."我也需要一个答案,然后意识到,“嘿,这不是一个难写的 npm 包。”

I don't believe there is a pure TS way to do this.我不相信有一种纯粹的 TS 方法可以做到这一点。 The package I created is called ts-json-as-const .我创建的包称为ts-json-as-const

npm install -D ts-json-as-const
npx ts-json-as-const ./path/to/file.json ./path/to/second/file.json
Example JSON file ( example.json )示例 JSON 文件 ( example.json )
{
  "foo": {
    "bar": false,
    "baz": true,
    "i-can": "hascheezburger"
  },
  "array": [ 1, 2, 3, { "foo": 1, "bar": [ 4, 5 ] }, 6 ]
}
Output example.json.d.ts输出example.json.d.ts
interface Example {
  foo: {
    bar: false;
    baz: true;
    'i-can': 'hascheezburger';
  },
  array: [
    1,
    2,
    3,
    {
      foo: 1;
      bar: [
        4,
        5
      ]
    },
    6
  ]
}

declare const Example: Example;

export = Example;

I'll admit, the array spacing isn't great, but who looks at .d.ts files for their json files anyway?我承认,数组间距不是很好,但是谁会查看 .d.ts 文件的 json 文件呢?

https://hackernoon.com/import-json-into-typescript-8d465beded79 https://hackernoon.com/import-json-into-typescript-8d465beded79

example.json:示例.json:

{
    "name": "testing"
}

javascript: javascript:

// ES6/ES2015
// app.js

import * as data from './example.json';

const word = data.name;

console.log(word); // output 'testing'

or In Typescript, however, the same code will throw error:或者在 Typescript 中,同样的代码会抛出错误:

Cannot find module 'example.json'找不到模块“example.json”

[UPDATE] Solution: Typescript 2.9 supports JSON import! [更新] 解决方案:Typescript 2.9 支持 JSON 导入!

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM