我有这样的代码:
const tokens = [
{
re: /^(#.*)$/,
parse: function(line: string) {
const style = styles.header;
return {
content: line,
style,
};
}
},
{
re: /^(__)([^_]+)(__)/,
parse: function(_:string, g1:string, text:string, g2:string) {
return {
content: text
};
}
}
];
const createChunks = (text: string) => {
return text.split('\n').map((line: string) => {
const chunk_parser = tokens.find(({re}) => re.test(line));
if (chunk_parser) {
const { parse, re } = chunk_parser;
const m = line.match(re);
if (m) {
return parse(...m);
}
}
return {
content: line
};
});
}
问题是我不能将字符串::Match的结果转换为数组,即使它是一个类似于数组的对象.NodeJS甚至在控制台中将其显示为array.我可以使用扩散运算符或Array.From将匹配结果隐藏为数组,但TypeScrip不允许这样做,并抛出以下错误:
扩散参数必须具有元组类型或传递给REST参数.
使用跨页时,以及:
"字符串[]"类型的参数不能赋给""字符串""类型的参数""
使用Array.From时.
这是NodeJS的输出:
> '# foo'.match(/^(#.*)$/)
[ '# foo', '# foo', index: 0, input: '# foo', groups: undefined ]
> [...'# foo'.match(/^(#.*)$/)]
[ '# foo', '# foo' ]