Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Typescript中编写sanitizer函数以检查空字符串、未定义字符串和空字符串,并在函数内部获取错误_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript 在Typescript中编写sanitizer函数以检查空字符串、未定义字符串和空字符串,并在函数内部获取错误

Javascript 在Typescript中编写sanitizer函数以检查空字符串、未定义字符串和空字符串,并在函数内部获取错误,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正在用Typescript编写一个泛型函数,以获取字符串值并检查它是否为null、未定义或空字符串并返回null,或者它是否为实际值并返回它 具有讽刺意味的是,我遇到的问题是,在函数的第二行return stringValue.trim()处我得到的错误对象可能为null或未定义,我不确定我可以做什么样的检查来修复它 这是我目前的职能: const sanitize = (stringValue: string | undefined | null) => { let emp

我正在用Typescript编写一个泛型函数,以获取字符串值并检查它是否为null、未定义或空字符串并返回null,或者它是否为实际值并返回它

具有讽刺意味的是,我遇到的问题是,在函数的第二行
return stringValue.trim()处
我得到的错误
对象可能为null或未定义
,我不确定我可以做什么样的检查来修复它

这是我目前的职能:

  const sanitize = (stringValue: string | undefined | null) => {
    let emptyString = '';
    let nullValue = null;
    let undefinedValue = undefined;

    if(typeof stringValue === emptyString || typeof stringValue === nullValue || typeof stringValue === undefinedValue){
      return null
    } else {
      return stringValue.trim();
    }
  }

这里不需要使用
typeof
,只需直接比较变量,这样TypeScript就知道您已经排除了那些可能的类型,从而保证
stringValue
不会
null
未定义

if(stringValue==''| | stringValue===null | | stringValue===未定义)

或者,正如@DBS所提到的,利用值并将您的语句简化为

if(!stringValue)

如果您还希望将仅包含空格的字符串返回为null,则可以将return语句更改为
return stringValue.trim()|null
,以检查修剪后的值是否变为
'


正如@DBS还提到的,
typeof
以字符串形式返回类型。另外,
null
对象上的
typeof
将返回
'object'
,而不是
'null'
,这使得此情况更加不适用。一般来说,我从未真正发现自己需要使用
typeof
,除了一些动态输入验证没有必要使用
typeof
,这里,只需直接比较变量,以便TypeScript知道您已经排除了那些可能的类型,它保证
stringValue
不会为
null
未定义

if(stringValue==''| | stringValue===null | | stringValue===未定义)

或者,正如@DBS所提到的,利用值并将您的语句简化为

if(!stringValue)

如果您还希望将仅包含空格的字符串返回为null,则可以将return语句更改为
return stringValue.trim()|null
,以检查修剪后的值是否变为
'


正如@DBS还提到的,
typeof
以字符串形式返回类型。另外,
null
对象上的
typeof
将返回
'object'
,而不是
'null'
,这使得此情况更加不适用。一般来说,我从未真正发现自己需要使用
typeof
,除了一些动态输入验证

而不是使用额外的变量和增加代码可读性,您的工作清理功能可以编写为

 const sanitize = (stringValue: string | undefined | null) => {
    if(typeof stringValue === "string"){
        return stringValue.trim();
    } else {
        return null
    }
  }
typeof-stringValue
将返回
string
如果类型为string,则
if
块将返回修剪过的字符串。如果类型不是字符串,我们只返回
null

正如@DBS所提到的,对于空字符串,上面的命令不会返回null。如果还希望为空字符串返回null,请使用以下版本:

 const sanitize = (stringValue: string | undefined | null) => {
    if(typeof stringValue === "string" && stringValue.trim().length>0){
        return stringValue.trim();
    } else {
        return null
    }
  }

和使用额外的变量和增加代码可读性不同,您的工作清理函数可以编写为

 const sanitize = (stringValue: string | undefined | null) => {
    if(typeof stringValue === "string"){
        return stringValue.trim();
    } else {
        return null
    }
  }
typeof-stringValue
将返回
string
如果类型为string,则
if
块将返回修剪过的字符串。如果类型不是字符串,我们只返回
null

正如@DBS所提到的,对于空字符串,上面的命令不会返回null。如果还希望为空字符串返回null,请使用以下版本:

 const sanitize = (stringValue: string | undefined | null) => {
    if(typeof stringValue === "string" && stringValue.trim().length>0){
        return stringValue.trim();
    } else {
        return null
    }
  }

typeof
返回的是,而不是类型本身。
typeof
返回的是,而不是类型本身。这是正确的,但有趣的是,由于JS的概念是“真实的”,你可以简单地说
如果(!stringValue){}
作为空/未定义和空字符串都是错误的。我试图接受答案,但上面说我得等10分钟haha@DBS是的,我只是觉得将代码保持在类似的格式更简单/更明显format@Josh没问题,哈哈,这些都是很好的读物,可以作为DBS的后续内容。正如你所说的那样,使消毒更有效easier@YousufKhan不确定OP是否需要这样一个案例,但这可以通过
返回stringValue.trim()| | null
轻松处理,这是正确的,但正如一个有趣的旁注,由于JS的概念是“真实的”,您可以简单地说
如果(!stringValue){}
因为空/未定义和空字符串都是假的。我试图接受答案,但它说我必须等待10分钟haha@DBS是的,我只是觉得将代码保持在类似的格式更简单/更明显format@Josh没问题,哈哈,这些都是很好的读物,可以作为DBS的后续内容。正如你所说的那样,使消毒更有效easier@YousufKhan不确定OP是否需要这样一个案例,但这可以通过
返回stringValue.trim()| | null
轻松处理。不幸的是,这没有抓住询问者想要为空字符串返回
null
的情况。啊,我的错,这也会通过编辑来覆盖该案例。谢谢:)不幸的是,这并没有抓住询问者想要返回空字符串的
null
的情况。啊,我的坏消息,将通过编辑来覆盖这种情况。谢谢:)