不同类型的嵌套对象数组的 Lodash concat
Lodash concat of arrays of nested objects of diffrent types
我想用 lodash concat 方法连接 2 个不同类型的嵌套对象数组。但是关于第二个数组类型的打字稿 linter 上升错误。
我的第一个数组是:
[
{
Header: '...',
accessor: '...',
minWidth: 200,
Filter: {...},
filter: {...},
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
Filter: {...},
filter: {...},
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
},
...
]
其中 Filter 和 filter 是大嵌套对象。我的第二个数组是:
[
{
Header: '...',
accessor: '...',
disableGroupBy: true,
}
]
我收到 typescript linter 错误
Type '{ Header: string; accessor: string; disableGroupBy: boolean; }' is missing the following
properties from type {...} : minWidth, Filter, filter.
请注意,没有汇总 属性 要求。
Lodash 文档中关于 concat 的描述:
Creates a new array concatenating array with any additional arrays
and/or values.
- 这是否意味着我只能连接平面对象或原始值?
- 我可以只连接相同类型的嵌套对象数组吗?
- 我应该使用不同的方法吗?
这样的话,用lodash
的concat
就可以了。
你得到 Type Error
的原因是因为 TypeScript 试图根据你的第一个数组推断你的第二个数组的类型,但这种类型推断并不总是正确的。
你可以在这里提供你自己的类型,或者如果你不关心返回数组的类型
就把它设置为any
// provide your type here. This type defines the shape of elements in the result array
concat<{Header: string, minwidth: number, ...}>(array1, array2)
// or set it to any
concat<any>(array1, array2)
我想用 lodash concat 方法连接 2 个不同类型的嵌套对象数组。但是关于第二个数组类型的打字稿 linter 上升错误。 我的第一个数组是:
[
{
Header: '...',
accessor: '...',
minWidth: 200,
Filter: {...},
filter: {...},
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
Filter: {...},
filter: {...},
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
Aggregated: () => null,
},
{
Header: '...',
accessor: '...',
minWidth: 200,
},
...
]
其中 Filter 和 filter 是大嵌套对象。我的第二个数组是:
[
{
Header: '...',
accessor: '...',
disableGroupBy: true,
}
]
我收到 typescript linter 错误
Type '{ Header: string; accessor: string; disableGroupBy: boolean; }' is missing the following properties from type {...} : minWidth, Filter, filter.
请注意,没有汇总 属性 要求。
Lodash 文档中关于 concat 的描述:
Creates a new array concatenating array with any additional arrays and/or values.
- 这是否意味着我只能连接平面对象或原始值?
- 我可以只连接相同类型的嵌套对象数组吗?
- 我应该使用不同的方法吗?
这样的话,用lodash
的concat
就可以了。
你得到 Type Error
的原因是因为 TypeScript 试图根据你的第一个数组推断你的第二个数组的类型,但这种类型推断并不总是正确的。
你可以在这里提供你自己的类型,或者如果你不关心返回数组的类型
就把它设置为any
// provide your type here. This type defines the shape of elements in the result array
concat<{Header: string, minwidth: number, ...}>(array1, array2)
// or set it to any
concat<any>(array1, array2)