【问题标题】:TypeScript add static helper to prototype of existing classTypeScript 将静态助手添加到现有类的原型
【发布时间】:2015-10-14 07:56:22
【问题描述】:

这主要是关于如何使用static 自定义方法添加/扩展任何现有类型的问题。

我想用一个函数扩展String 原型,例如isNullOrEmpty 应该以C# 方式调用:

if(!String.isNullOrEmpty(myStringToCheck)){
    // do things as of myStringToCheck is set to something
}

在普通的 javascript 中,我可以做类似的事情

String.isNullOrEmpty = function (s) {
    if (s == null || s === "")
        return true;

    return false;
}

但是,当在 TypeScript 中调用它时,它会告诉我

属性“isNullOrEmpty”不存在于类型“{原型:字符串; fromCharCode(...codes: number[]): string; (值?:任何):字符串;新(值?:任何):字符串; }'。

如何才能让TypeScript 知道它?

编辑#1

TypeScript 已经知道的String.fromCharCode() 是如何实现的?

编辑#2

由于项目中的其他依赖项,我目前只允许使用TypeScript 1.0

编辑#3

String.d.ts

interface StringConstructor {
    isNullOrEmpty(): boolean;
}

interface String {
    format(...args: any[]): string;
    isNullOrEmpty(): boolean;
} 

还有我的String.ts

/// <reference path="../../../typings/String.d.ts"/>

String.prototype.format = function (): string {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp("\\{" + i + "\\}", "gi");
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
}

String.isNullOrEmpty = function(s) { <-- here is the exception
    if (s == null || s === "")
        return true;
    return false;
}

解决方案 #1TypeScript 版本 > 1.0?)

查看MartyIX的第一个答案

解决方案 #2TypeScript 1.0 版的解决方法)

查看第二个答案

【问题讨论】:

  • 您应该用绿色勾号标记您的答案。 :)

标签: javascript typescript prototype


【解决方案1】:
// String.ts
interface StringConstructor {
    isNullOrEmpty(text: string): boolean;
}

interface String {
    isNullOrEmpty(text: string): boolean;
}

String.isNullOrEmpty = (s:string):boolean => {
    return (s == null || s === "");
};

// OtherFile.ts
///<reference path="path/to/String.ts" />

if(!String.isNullOrEmpty("test")){
    // Do something
}

var isEmpty = String.fromCharCode(100).isNullOrEmpty("Nah");

这对我有用。接口扩展StringConstructor

【讨论】:

  • 我将如何实现该方法?我有一个String.ts,其中我已经实现了更多扩展(但目前只有新功能而不是static 功能)。
  • 请查看编辑后的答案。你不需要在这里做任何花哨的事情。做你在纯 JavaScript 中会做的事情。
  • 这不起作用。还是说The property 'isNullOrEmpty' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'. 可能是版本问题?
  • 最新版本怎么样?
  • 好吧,如果您将我的代码发送到typescriptlang.org/Playground,它就可以工作。不幸的是,我没有 TypeScript 1.0,它相当旧。
【解决方案2】:

正如@MartyIX 的 cmets 所说,TypeScript 1.0 似乎有问题。 作为一种解决方法,我在String 的原型中添加了一个方法:

String.d.ts(定义)

interface String {
    isNullOrEmpty(text: string): boolean;
} 

String.ts(实现)

String.prototype.isNullOrEmpty = function(text) => {
    if (text == null || text === "")
        return true;

    return false;
}

可以使用的

if(!String.prototype.isNullOrEmpty(myStringToCheck)){
    // do things as of myStringToCheck is set to something
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多