使用 replacerFunction 回调时重载时出现编译错误
compilation error at overload when using replacerFunction callback
我有一个生成随机词的函数:
const randomWord = () => {
return "obbooobb".replace(/([ob])/g, (match: string) => {
if (match === "o") {
// something
}
if (match === "b") {
// something
}
});
};
任何时候 deno
在启动运行时之前启动检查都会抛出以下错误:
Check file:///root/index.ts
error: TS2769 [ERROR]: No overload matches this call.
The last overload gave the following error.
Argument of type '(match: string) => string | undefined' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
return "obbooobb".replace(/([ob])/g, (match: string) => {
~~~~~~~~~~~~~~~~~~~~
at file:///root/controllers/data.ts:66:60
TS2771 [ERROR]: The last overload is declared here.
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at asset:///lib.es5.d.ts:456:5
- deno 1.21.2(发布,x86_64-unknown-linux-gnu)
- v8 10.0.139.17
- 打字稿 4.6.2
从最初的搜索我或多或少意识到这是个问题。我看到了与此问题相关的其他 Whosebug
和 git issues
主题,但我没有找到最简单的方法来解决这个问题,这对我有帮助,也许有人已经遇到过类似的问题并以简单的方式解决了。
您没有在传递给 replace 的回调上指定 return 类型,并且正如所写的那样,它不提供 return 值,因此推断为未定义。指定 return 类型,或确保所有代码路径 return 为预期类型(字符串)
的值
我有一个生成随机词的函数:
const randomWord = () => {
return "obbooobb".replace(/([ob])/g, (match: string) => {
if (match === "o") {
// something
}
if (match === "b") {
// something
}
});
};
任何时候 deno
在启动运行时之前启动检查都会抛出以下错误:
Check file:///root/index.ts
error: TS2769 [ERROR]: No overload matches this call.
The last overload gave the following error.
Argument of type '(match: string) => string | undefined' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
return "obbooobb".replace(/([ob])/g, (match: string) => {
~~~~~~~~~~~~~~~~~~~~
at file:///root/controllers/data.ts:66:60
TS2771 [ERROR]: The last overload is declared here.
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at asset:///lib.es5.d.ts:456:5
- deno 1.21.2(发布,x86_64-unknown-linux-gnu)
- v8 10.0.139.17
- 打字稿 4.6.2
从最初的搜索我或多或少意识到这是个问题。我看到了与此问题相关的其他 Whosebug
和 git issues
主题,但我没有找到最简单的方法来解决这个问题,这对我有帮助,也许有人已经遇到过类似的问题并以简单的方式解决了。
您没有在传递给 replace 的回调上指定 return 类型,并且正如所写的那样,它不提供 return 值,因此推断为未定义。指定 return 类型,或确保所有代码路径 return 为预期类型(字符串)
的值