简体   繁体   中英

typescript: Import only type from json file

Is there any way to import just JSON file type ? I have a complex JSON file and I need it's definition to be used in whole application, but when I do import * as data from 'data.json' , whole file is imported, which causes its presence in build (using webpack). As far as I know, there is no way to exclude it in webpack config .

EDIT: My question might be a little bit unclear, so here is my example:

import * as data from 'data.json'
// data looks like this:
// { 
//   a: {
//    b: 'something1'
//   },
//   c: {
//    d: 'something2'
//   }
// } 

export type typedJson = typeof data;
// which should show on hover/autocomplete: { "a": { "b": "string" }, "c": { "d": "string" } }

But when I do that, whole data.json file is imported and it's present in webpack bundle. Is there any way to have those typings and exclude it from bundle?

您可以使用导入类型来获取json文件的类型:

type JsonDataType = typeof import('./data.json')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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