TypeScript 向现有类的原型添加静态助手 [英] TypeScript add static helper to prototype of existing class

查看:67
本文介绍了TypeScript 向现有类的原型添加静态助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is mainly a question on how to add/extend any existing type with static custom methods.

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

I want to have the String prototype extended with a function, e.g. isNullOrEmpty which should be invoked the C# way:

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

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

In plain javascript I could do something like

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

    return false;
}

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

But, when calling it inside a TypeScript it tells me

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

The property 'isNullOrEmpty' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'.

如何做到这一点才能让 TypeScript 知道?

How can this be done so it is known by TypeScript?

编辑 #1

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

How is String.fromCharCode() implemented which is already known by TypeScript?

编辑 #2

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

Because of other dependencies in the project I'm currently only allowed to use 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;
}

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

Solution #1 (TypeScript version > 1.0?)

查看 MartyIX 的第一个回答

see first answer by MartyIX

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

Solution #2 (workaround for TypeScript version 1.0)

看第二个答案

推荐答案

根据@MartyIX 的评论,TypeScript 1.0 似乎存在问题.作为一种解决方法,我在 String 的原型中添加了一个方法:

As by comments from @MartyIX, it seems to be a problem with TypeScript 1.0. As a workaround I've added a method to the prototype of 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
}

这篇关于TypeScript 向现有类的原型添加静态助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆