簡體   English   中英

基本類型字符串上的TypeScript重載方法

[英]TypeScript overloading method on base-type string

TypeScript版本:1.6

我正在嘗試為Stringreplace -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)

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;

searchValueRegExp ,編寫的內容不兼容,因此您需要使用聯合類型來更改函數簽名以同時允許stringRegExp

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM