这是对象,它从类中的方法返回:
public dbParameters() // HERE
{
return {
"values": this.valuesForDb,
"keys": this.keysForDb,
"numbers": this.numberOfValues,
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您能否建议如何定义函数返回的类型?或者这可能不是正确的方法,我应该使用另一种类型而不是对象文字?
一种方式可能只是一个消息,结果是一个dictinary:
public dbParameters() : { [key: string]: any}
{
return {
"values": this.valuesForDb,
"keys": this.keysForDb,
"numbers": this.numberOfValues,
}
}
Run Code Online (Sandbox Code Playgroud)
另一个可以使用一些界面
export interface IResult {
values: any[];
keys: string[];
numbers: number[];
}
export class MyClass
{
public dbParameters() : IResult
{
return {
values: this.valuesForDb,
keys: this.keysForDb,
numbers: this.numberOfValues,
}
}
}
Run Code Online (Sandbox Code Playgroud)
随着interface我们有很大的优势...它可以在很多地方都可以重复使用(声明,用法......)所以这将是最好的一个
而且,我们可以组成属性结果的最具体设置
export interface IValue {
name: string;
value: number;
}
export interface IResult {
values: IValue[];
keys: string[];
numbers: number[];
}
Run Code Online (Sandbox Code Playgroud)
在这里玩
| 归档时间: |
|
| 查看次数: |
1968 次 |
| 最近记录: |