[英]How to Import JSON file and cast it
我加載了一個 JSON 文件,以響應如下導入語句:
import myStrings from './date.json';
JSON結構非常簡單:
[
{
"type": 1,
"text": "mytext",
"during": 5,
}
]
現在我找不到任何方法來投射這個集合中的一個項目,以便在 function 參數中使用。 我不想使用任何類型:
const ret = myFunction(myStrings[0]);
const myFunction = (astringParameter:any):string => { ... }
這個項目怎么投?
像這樣定義一個接口。
interface MyString {
type: number,
text: string,
during": number,
}
const ret: MyString = myFunction(myStrings[0]);
const myFunction = (astringParameter:MyString):string => { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.