typescript 如何设置属性的结构化值?

f4t66c6m  于 2023-01-06  发布在  TypeScript
关注(0)|答案(1)|浏览(130)

我有一个返回2个值的简单函数,我希望我可以将函数的返回值分解为filters对象中的sortsortOrder,但如果我这样做,我会得到错误:
类型"DashboardSortingPreferences"上不存在属性"sort"。

const { this.filters.sort, this.filters.sortorder } = this.dashboardColumnSortingService.initializeDashboardSortProperties(
    this.user,
    this.dashboardName,
);
const { sorted_column, direction } = this.dashboardColumnSortingService.initializeDashboardSortProperties(
    this.user,
    this.dashboardName,
);

this.filters.sort = sorted_column;
this.filters.sortorder = direction;

这是可行的,我理解为什么,但我想知道是否可以直接在sortsortOrder属性上设置sort_columndirection

quhf5bfb

quhf5bfb1#

Related answer.
这是可能的。在mdn web docs上我们可以看到:

const numbers = [];
const obj = { a: 1, b: 2 };
({ a: numbers[0], b: numbers[1] } = obj);
// The properties `a` and `b` are assigned to properties of `numbers`

因此,在您示例中,它看起来如下所示:

({ sorted_column: this.filters.sort, direction: this.filters.sortorder } = 
this.dashboardColumnSortingService.initializeDashboardSortProperties(
this.user,
this.dashboardName));

相关问题