有没有办法将 isNullOrEmpty(str:string) 添加到静态字符串对象。
这样我就可以称它为:
String.isNullOrEmpty(myobj);
我找到了一种将其放在实现中的方法,但这对此类方法没有帮助。
最佳答案
String
在lib.d.ts
中定义,接口(interface)如下
interface StringConstructor {
...
}
declare var String: StringConstructor;
所以虽然你不能将方法添加到变量中,但你可以将它们添加到接口(interface)中,使用
interface StringConstructor {
isNullOrEmpty(str:string):boolean;
}
然后他们在变量上实现它们,使用
String.isNullOrEmpty = (str:string) => !str;
关于 typescript 扩展字符串静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17843215/