我想用打字稿定义一个对象,该对象具有一个名为property
的字段。
export interface Input {
documents: Array<DocumentInput>;
}
export interface DocumentInput {
properties?: {
[key: string]: object;
};
}
目前,我正在这样做以定义属性。
const docProperties = {};
docProperties['name'] = 'ABC';
docProperties['description'] = 'PQR';
let request: Input = {
documents :[
{
properties:docProperties
}]
}
我想减少行数,改写这样的内容。
let request: Input = {
documents :[
{
properties:
{
"name" : "ABC",
"description" : "PQR"
}
}]
}
我该怎么做?
答案 0 :(得分:1)
您可以定义为
export interface Input {
documents: any; // or object
}
export interface DocumentInput {
properties?: any; // or object
}
并与
一起使用 let request: Input = {
documents :[
{
properties:
{
"name" : "ABC",
"description" : "PQR"
}
}]
}
console.log(request.documents[0].properties.name)
//or
console.log(request.documents[0].properties['name'])