San*_*ord 4 destructuring typescript
我正在破坏正则表达式匹配的结果
function getStuffIWant(str: string): string {
const [
fullMatch, // [ts] 'fullMatch' is declared but its value is never read.
stuffIWant,
] = str.match(/1(.*)2/);
return stuffIWant;
}
getStuffIWant("abc1def2ghi");
Run Code Online (Sandbox Code Playgroud)
正如评论所指出的,fullMatch从未使用过,TSC希望我知道。 有没有办法在不关闭所有未使用支票的情况下抑制此错误?
我也尝试将数组解压缩为一个对象:
const {
1: stuffIWant, // Unexpected SyntaxError: Unexpected token :
} = str.match(/1(.*)2/);
Run Code Online (Sandbox Code Playgroud)
几乎立即找到了答案(并非总是如此)-解构数组时,可以通过在以下位置添加一个额外的逗号来忽略选择值:
function getStuffIWant(str: string): string {
const [
, // full match
stuffIWant,
] = str.match(/1(.*)2/);
return stuffIWant;
}
getStuffIWant("abc1def2ghi");
Run Code Online (Sandbox Code Playgroud)
没有声明任何变量,TypeScript没有任何准备。
| 归档时间: |
|
| 查看次数: |
394 次 |
| 最近记录: |