[英]Typescript method overloading with one parameter of different type (non-primitive)
[英]TypeScript overloading method on base-type string
TypeScript版本:1.6
我正在尝试为String
的replace
-function添加另一个重载函数,该函数应具有以下签名:
replace(searchValue: string, replaceValue: any): string;
所以它可以像
"someString".replace("replaceMe", $(evt.target).data("row-id"))
我需要any
.data
定义为:
data(key: string): any;
我的String.d.ts
(声明)
interface String {
/**
* Replaces text in a string, using a regular expression or search string.
* @param searchValue A string that represents the regular expression.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replace(searchValue: string, replaceValue: any): string;
}
我的String.ts
(实现)
String.prototype.replace = (searchValue: string, replaceValue: any):string =>
{
if (replaceValue instanceof String) {
var stringReplacement = replaceValue.toString();
return String.prototype.replace(stringReplacement, replaceValue);
} else {
return replaceValue;
}
}
引发以下错误,但我不确定为什么以及如何解决该错误:
错误TS2322类型'((searchValue:字符串,replaceValue:任意)=>字符串)无法分配给类型'{{(searchValue:字符串,replaceValue:字符串):字符串; (searchValue:字符串,replacer :(子字符串...”。参数“ searchValue”和“ searchValue”的类型不兼容。类型“ string”不可分配给“ RegExp”类型。类型“ String”中缺少属性“ exec” '。
编辑#1
最后,我只声明了replace
(和删除实现)来满足编译器的要求:
interface String {
/**
* Replaces text in a string, using a regular expression or search string.
* @param searchValue A string that represents the regular expression.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replace(searchValue: string, replaceValue: any): string;
}
从内部来看,这仍然是错误的,它仍然调用库的replace
function吗?
编辑#2
如果不使用自己的Visual Studio大喊大叫
使用演员表显示(ReSharper)
带有示例4.1(MartyIX)
进行"someString".replace("replaceMe", $(evt.target).data("row-id"));
应该可以工作,因为可以将any
分配给string
。 我不确定为什么会出错,但是我建议确保尚未从lib.d.ts
删除具有string
作为replace
第一个参数的函数签名。 您可以通过转到replace的定义(将光标放在replace上并单击F12)并确保它具有下面显示的所有功能签名来再次检查。 您也可以尝试暂时禁用重新共享器,以查看该错误是否消失。
顺便说一下,要通过覆盖String.prototype.replace
来修复错误,它已经具有以下可能的函数签名:
replace(searchValue: string, replaceValue: string): string;
replace(searchValue: string, replacer: (substring: string, ...args: any[]) => string): string;
replace(searchValue: RegExp, replaceValue: string): string;
replace(searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string;
当searchValue
是RegExp
,编写的内容不兼容,因此您需要使用联合类型来更改函数签名以同时允许string
和RegExp
:
String.prototype.replace = function(searchValue: string | RegExp, replaceValue: any) {
// omitted
}
我不确定您要做什么,因为该函数将执行无限循环。 也许您打算保留对原始String.prototype.replace
的引用并调用它? 请记住,当您分配给String.prototype.replace
时,您将覆盖原始函数。
总的来说,尽管不做这里正在做的事情 。 查找“不要修改您不拥有的对象”以了解我的意思。 如果创建自己的单独函数来执行此操作会更好,因为现在此replace函数会破坏使用该函数的所有代码 。 如果您使用的任何库都使用replace
方法,那么您将获得真正的怪异行为,这将很难跟踪问题。
我对此:
1)以下代码段未报告我的计算机上的任何错误:
/// <reference path="typings/jquery/jquery.d.ts" />
"someString".replace("replaceMe", $('#test').data("row-id"));
您确定首先存在问题吗? 您对TypeScript编译器的设置是什么(即,如何编译TS代码)?
2)如果需要实现replace
方法,则应真正考虑为此创建自己的模块,否则可能会使阅读代码的人感到困惑:
module Project.Utils {
export class String {
public replace = (searchValue: string, replaceValue: any):string =>
{
// some replace implementation
}
}
}
3)首先,实现是错误的,因为您替换了String.prototype.replace
,然后希望它会调用String.prototype.replace
的本机实现,但这不是正确的。 实际上,您的实现根本不执行任何替换功能。
String.prototype.replace = (searchValue: string, replaceValue: any):string =>
{
if (replaceValue instanceof String) {
var stringReplacement = replaceValue.toString();
return String.prototype.replace(stringReplacement, replaceValue);
} else {
return replaceValue;
}
}
4)这些代码在http://www.typescriptlang.org/Playground上运行得很好:
let n:any = 1;
"someString".replace("replaceMe", n);
"someString".replace("replaceMe", <any>5); // explicit cast
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.