gpt4 book ai didi

javascript - typescript 类不接受参数类型

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:41 27 4
gpt4 key购买 nike

我正在 Angular 中为数据模型创建新类。

我已经在新文件中创建类并在构造函数中初始化它们

export class CreditPoliciesDetails { 
private _fileName : string ="" ;
private _fileDate : string ="" ;
private _fileSize : string ="" ;
private _path : string="";
private _extention: string="";
constructor() {
this._fileDate="fileDate";
this._fileName="fileName";
this._fileSize="fileSize";
this._path="path";
this._extention="extension";
}
}

export class CreditPolicies {
private _document : CreditPoliciesDetails[];
private _directory: string
constructor (details: CreditPoliciesDetails[]) {
this._document = details;
this._directory="";
}
get category() {
return this._document
}
public details (detail) {
console.log(detail)

this._document= detail;
}
}

然后在我的组件 ts 文件 中声明新的 CreditPoliciesDetails数组然后将其传递给 files这是 CreditPolicies 类型的数组

  details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); 

files : CreditPolicies[] = new Array <CreditPolicies>(this.details);

在 VS 代码中,当我悬停 new Array <CreditPolicies>(this.details); 的参数时,我收到警告Argument of type 'CreditPoliciesDetails[]' is not assignable to parameter of type 'CreditPolicies'.
Property '_document' is missing in type 'CreditPoliciesDetails[]'.

当我 console.log filesdetails我得到空数组。

我期待这种格式

[  
{
"directory":"string",
"document":[{
"extension":"string",
"lastModifiedDate":"string",
"name":"string",
"path":"string",
"size":"0"
}]
}
]

最佳答案

您必须调用构造函数,而不是将 CreditPoliciesDetails 数组转换为 CreditPolicies 数组

details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); 

//files : CreditPolicies[] = new Array <CreditPolicies>(this.details);

files : CreditPolicies = new CreditPolicies(this.details);

关于javascript - typescript 类不接受参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49917139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com