2015-10-15 90 views
0

打字稿-版本:1.6打字稿上基本字符串类型重载方法

我想添加另一个重载函数为String小号replace - 函数应该具有以下特征:

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 Type'(searchV alue:string,replaceValue:any)=> string'不可分配给类型'{(searchValue:string,replaceValue:string):string; (searchValue:string,replacer:(substring ...') 参数'searchValue'和'searchValue'的类型不兼容 类型'string'不能分配给'RegExp'类型 '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功能?

编辑#2

如果使用没有自己的Visual Studio大喊大叫

Withour cast

随着投它显示(ReSharper的)与实例4.1(

ReSharper says

MartyIX)

With variable declaration

+0

所以,我不知道打字稿什么,但我看不出有什么这个方法做,你已经不能用'String.prototype.replace'做什么? – Mathletics

+0

似乎TS函数对参数类型不变,而'f(string,any)'不能保存为'f(string,string)'类型的值。 –

+0

@Mathletics它在编译时得到了一个错误,因为在typescript中没有'replace(string,any)的定义' – KingKerosin

回答

2

"someString".replace("replaceMe", $(evt.target).data("row-id"));应该工作,因为any可以分配给string。我不确定为什么会出现错误,但我会建议确保string的功能签名作为replace的第一个参数尚未从lib.d.ts中删除。您可以通过转到replace的定义(将光标放在替换上并按F12)并确保它具有下面显示的所有功能签名来仔细检查。您也可以尝试暂时禁用resharper,以查看是否会导致错误消失。

顺便说一句,与覆盖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; 

写着什么是不能当searchValueRegExp兼容,所以你需要改变功能签名同时允许stringRegExp利用共用类型:

String.prototype.replace = function(searchValue: string | RegExp, replaceValue: any) { 
    // omitted 
} 

我不知道你正在试图做的,但因为该函数将做一个什么无限循环。也许你打算参考原始String.prototype.replace并打电话给那个?请记住,当您指定String.prototype.replace时,您将覆盖原始功能。

总体来说虽然不做这里做的是什么。看看“不要修改你不属于自己的物体”来看看我的意思。如果你创建你自己的独立函数来做到这一点会更好,因为现在这个替换函数会打破使用它的任何代码。如果您使用的任何库使用replace方法,那么您将会遇到非常奇怪的行为,并且很难追踪该问题。

+0

我不明白你所描述的是什么。不接触我不拥有的物体是完全合理的。在我的问题中,我已经用'编辑#1'结束了。这个可以吗? 至少我不想打电话给'。toString()'每次满足编译器 – KingKerosin

+1

在我看来,你应该创建一个独立于'interface String'的函数,它可以做你想做的事情。例如'StringUtils.myCustomReplaceFunction(“someString”,“replaceMe”,$(evt.target).data(“row-id”));'。 –

0

我拿到这个:

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 
+0

1)它显示了VS中描述的错误。我的设置是在%programfiles%中使用TSCompiler 1.6,或者你是指其他一些设置? 2)好点,但我的编辑(这使得3)过时)仍然没有调用正确的“内部”替换? 3)我删除了我自己的实现 4)让我检查 – KingKerosin

+0

1)您已经显示了_own_实现的错误。不是实际的命令:''someString“.replace(”replaceMe“,$(evt.target).data(”row-id“))' –

+0

3)当你删除你自己的实现时,它很好。所以编辑#1是一个正确的解决方案。 –