[英]Typescript/javascript extension method that works with null and undefined
In .net/c#, you can define an extension method like this:在 .net/c# 中,你可以定义这样的扩展方法:
public static bool IsBlank(this string s)
{
return String.IsNullOrEmpty(s);
}
and call it like this:并这样称呼它:
string myString = null
if(myString.IsBlank()) ...
This is extremely useful for dealing with strings that may be null.这对于处理可能为空的字符串非常有用。
In Typescript and javascript, I would very much like to do the same thing, something like this:在 Typescript 和 javascript 中,我非常想做同样的事情,就像这样:
String.prototype.isBlank = function ()
{
return this === null || this === undefined || this == '';
}
var myString: string = null
if(myString.isBlank()) ...
but it doesn't work, because javascript doesn't know anything about the fact that myString
is typed as string
, or was in the Typescript file, anyway.但它不起作用,因为无论如何 javascript 都不知道
myString
被键入为string
或在 Typescript 文件中这一事实。 So you get an error like 'isBlank' is not a member of null
.所以你会得到一个错误,比如
'isBlank' is not a member of null
。
The only real workaround is something klunky like if((mystring || '').isNotBlank())
, but that gets tedious (and error prone).唯一真正的解决方法是像
if((mystring || '').isNotBlank())
,但这会变得乏味(并且容易出错)。
In javascript, is there any way to extend null itself so that a call like null.coerce(defaultValue)
would work?在 javascript 中,有什么方法可以扩展 null 本身,以便像
null.coerce(defaultValue)
这样的调用可以工作?
If not, is there a way to cause typescript to transpile null.coerce(defaultValue)
into (null || defaultValue)
?如果没有,有没有办法让打字稿将
null.coerce(defaultValue)
转换为(null || defaultValue)
?
Update: I know I could always write coerce(variable, defaultValue)
, but I think that's an even more klunky syntax.更新:我知道我总是可以写
coerce(variable, defaultValue)
,但我认为这是一种更加笨拙的语法。
Update 2 : My simple code snippet was too simple to illustrate the use case, as vanilla javascript has simple workarounds, as some answers said.更新 2 :我的简单代码片段太简单了,无法说明用例,因为正如一些答案所说,vanilla javascript 有简单的解决方法。
In a real application, suppose I get a complex table object back from some API call, and I want to write something like:在实际应用程序中,假设我从某个 API 调用中获取了一个复杂的表对象,并且我想编写如下内容:
var x = result.rows[i].field[j].parseAsInt(0).toString()
because rows(i, j) is some legacy field that's an integer, but they stored it as a string so they could use "-" instead of null or zero, but sometimes they DO use null, and I have to deal with it by mapping any such thing to zero.因为行(i,j)是一些遗留字段,它是一个整数,但他们将它存储为一个字符串,所以他们可以使用“-”而不是空或零,但有时他们确实使用空,我必须通过将任何这样的东西映射到零。 My
parseAsInt
method would be something that says "if it's parseable as an integer, return the integer, otherwise ("-", null, undefined) the specified default value. The alternative would be我的
parseAsInt
方法会说“如果它可以解析为整数,则返回整数,否则 ("-", null, undefined) 指定的默认值。替代方法是
var x = (result.rows[i].field[j] || 0).parseAsInt(0).toString()
and that is a more awkward syntax.这是一种更尴尬的语法。
That is beyond the capabilities of TypeScript at present.这超出了目前 TypeScript 的能力。
It should be noted that this specific extension method you're wanting to create is probably not needed;应该注意的是,可能不需要您要创建的这种特定扩展方法; in JS both
null
and ''
are falsy, so you really only need to do:在 JS 中
null
和''
都是假的,所以你真的只需要这样做:
if (myString) {
// ...
}
...to get the behavior you illustrated. ...获得您所说明的行为。 In other cases, the
?.
在其他情况下,
?.
operator is a handy way to handle attempts to call null and undefined values, but there still isn't the ability to do a true extension method, and null
has no prototype so you can't use a prototype extension either.运算符是处理尝试调用 null 和 undefined 值的便捷方法,但仍然无法执行真正的扩展方法,而且
null
没有原型,因此您也不能使用原型扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.