[英]Import Json file in Typescript : Error in terminal
I am new in typescript and I have 2 questions. 我是打字稿新手,有2个问题。
a. 一种。 I want to import a simple JSON file in my typescript (main.ts) using the following code in visual studio code (3.6.2) editor:
我想在Visual Studio代码(3.6.2)编辑器中使用以下代码在我的打字稿(main.ts)中导入一个简单的JSON文件:
import test from "./test.json";
console.log(test.name);
I also tried with import * as test from "./test.json";
我也尝试使用
import * as test from "./test.json";
While the JSON file (test.json) is the following: JSON文件(test.json)如下:
{"name": "testing",
"age":25}
However, when I am typing "test.", typescript is giving the options for "name", "age" as the properties. 但是,当我键入“ test。”时,打字稿将“名称”,“年龄”的选项作为属性提供。
And the tsconfig.json file is following: tsconfig.json文件如下:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
}
}
This code is throwing the following error: 该代码引发以下错误:
Cannot find module './test.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
Since the type definition is for old versions, I haven't used that. 由于类型定义是针对旧版本的,因此我没有使用过。 My typescript version is 3.6.3.
我的打字稿版本是3.6.3。 The above mentioned 3 files are inside the same folder.
上面提到的3个文件位于同一文件夹中。
-testfolder
- main.ts
- tsconfig.json
- test.json
Though there are some other questions similar to this, but I couldn't find the problem in my code! 尽管还有其他一些与此类似的问题,但是我在代码中找不到问题! I have also tested that with submile text 3 but it's also giving the same error!
我也用submile文字3进行了测试,但是它也给出了相同的错误!
b. b。 If I want to parse a json data from a file where the file extension is different (not .json), how should I do that?
如果我想从文件扩展名不同的文件(不是.json)中解析json数据,该怎么办?
Thank you in advance for your time. 预先感谢您的宝贵时间。
假设您使用VSCode并且所有文件都在同一目录中,并且直接从该目录打开VSCode,则您的tsconfig可以完美运行(至少适用于TS 3.6)。
try using a module. 尝试使用模块。 Review this documentation https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
查看此文档https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
// index.ts // index.ts
import file from "./file.json";
console.log(file);
console.log(file.name);
// json.dt.ts // json.dt.ts
declare module "*.json" {
const file: any;
export default file;
}
Nihar, 尼哈尔
I was able to reproduce the problem you mention. 我能够重现您提到的问题。 I was also able to resolve the problem using the following tsconfig.json :
我还可以使用以下tsconfig.json解决问题:
{
"compilerOptions": {
/* Basic Options */
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true
},
"files": ["main.ts"]
} }
Of course, the added "files" section assumes your main file is named "main.ts". 当然,添加的“文件”部分假定您的主文件名为“ main.ts”。 Frankly I'm not sure why this works and your original does not, but when I use a tsconfig.json file, I try to put everything typescript needs to know in that one place.
坦白地说,我不确定为什么它会起作用,而您的原始文章却不起作用,但是当我使用tsconfig.json文件时,我尝试将打字稿需要知道的所有内容都放在那个地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.