【发布时间】:2019-02-03 10:17:08
【问题描述】:
所以我有这段代码可以找到 x 子数组的所有可能组合:
const optionGroups: string[][] = [['Beer', 'Not beer'], ['1', '2', '11']];
const combinations = optionGroups.reduce((opt1, opt2) => {
return opt1.reduce((acc, option1) => {
return acc.concat(opt2.map(option2 => {
return (<string[]>[]).concat(option1, option2);
}));
}, []);
});
return combinations; // <-- This is not a string[]
然而 Typescript 认为返回类型是 string[],而实际上它是 string[][]。
输出为[['1', 'Beer'], ['2', 'Beer']...]等
我终其一生都无法弄清楚如何修复这种类型。你可以自己试试sn-p看看。
即使转换它也会产生错误:
类型“string[]”无法转换为类型“string[][]”。类型 'string' 不能与类型 'string[]' 比较。
任何指针?
【问题讨论】:
标签: typescript types reduce