2015-10-15 91 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是一個正確的解決方案。 –