简体   繁体   English

'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误

[英]Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error

TS error "Object is undefined" TS 错误“对象未定义”

Trying to access "userid" from my headers.试图从我的标题访问“用户 ID”。
It keeps throwing the error "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'."它不断抛出错误“'string | undefined'类型的参数不可分配给'string'类型的参数。'undefined'类型不可分配给'string'类型。”

在此处输入图像描述

不明确的

When I have already defined userid as string or undefined why does this error pop up??当我已经将用户标识定义为字符串或未定义时,为什么会弹出此错误?
Any idea anyone????有人知道吗???

Update:更新:
Adding a check before accessing apiGateway event在访问 apiGateway 事件之前添加检查

if (apiGateway.event !== undefined) {
   const { userid } = req.apiGateway.event.headers;
} else {
   throw new badRequestException() 
}

return......

Judging by your definition of MyRequest :从您对MyRequest的定义来看:

  • apiGateway can be undefined apiGateway可以未定义
  • event cannot be undefined event不能未定义
  • headers cannot be undefined headers不能未定义
  • userid can be undefined userid可以是未定义的

So right off the bat, writing req.apiGateway.event.headers will fail, because if apiGateway is undefined, then accessing .event on it will crash.所以马上,写req.apiGateway.event.headers会失败,因为如果apiGateway未定义,那么访问.event就会崩溃。 So Typescript does not allow that.所以 Typescript 不允许这样做。

There are three ways around this:有三种方法可以解决这个问题:

  1. Explicitly check against it.明确地检查它。 Something like if (req.apiGateway === undefined) { throw new badRequestException(); }类似if (req.apiGateway === undefined) { throw new badRequestException(); } if (req.apiGateway === undefined) { throw new badRequestException(); }
  2. If you're certain that it can't be undefined , you can force TypeScript to accept it.如果你确定它不能是undefined ,你可以强制 TypeScript 接受它。 Write req.apiGateway..event .编写req.apiGateway..event Mind you, if apiGateway does happen to be undefined at runtime, you'll get an exception.请注意,如果apiGateway确实在运行时未定义,您将收到异常。 So only use this if you are absolutely 100% sure that it cannot under any circumstance be undefined.因此,只有在您绝对 100% 确定它在任何情况下都不能未定义时才使用它。
  3. Accept that it can be undefined and coerce everything else to undefined as well.接受它可以是未定义的,并将其他所有内容强制为未定义。 Write req.apiGateway?.event - in this case .event will also be considered as undefined-able.req.apiGateway?.event - 在这种情况下.event也将被认为是未定义的。 If req.apiGateway happens to be undefined in runtime, then req.apiGateway?.event will also be undefined.如果req.apiGateway恰好在运行时未定义,那么req.apiGateway?.event也将未定义。 And so on and so forth.等等等等。 This means that you'll also have to add ?.这意味着您还必须添加?. to the rest of the line: req.apiGateway?.event?.headers .到该行的 rest: req.apiGateway?.event?.headers And the result of the whole expression is ALSO undefined-able, so you'll probably need to use more undefined-checks later.并且整个表达式的结果也是未定义的,因此您以后可能需要使用更多的未定义检查。

Now, for your second problem.现在,对于您的第二个问题。 Currently your userid local variable has the type string|undefined .当前,您的userid局部变量的类型为string|undefined That us because req.apiGateway.event.headers.userid is string|undefined .我们是因为req.apiGateway.event.headers.useridstring|undefined This means that at runtime it can be either a string, or undefined.这意味着在运行时它可以是字符串,也可以是未定义的。 However the method updateNotification() expects a simple string as its parameter.然而方法updateNotification()需要一个简单的string作为它的参数。 It cannot deal with undefined values.它不能处理未定义的值。 If you were to pass it undefined, who knows what would happen (probably an exception).如果你要传递它未定义,谁知道会发生什么(可能是一个例外)。 Therefore Typescript does not allow this.因此 Typescript 不允许这样做。 And, again, you can use the above methods for dealing with it.而且,同样,您可以使用上述方法来处理它。

always when u got this error it's good to just check if varible is:总是当你得到这个错误时,最好检查一下变量是否是:

if(!varible) return //or throw new Error();
//other stuff

after this if type of varible will be string instead of string |在此之后,如果变量的类型将是字符串而不是字符串 | undefind未定义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 typescript '严格' - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict' Typescript:类型参数'string | undefined' 不能分配给“string”类型的参数 - Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 'string' Typescript 枚举键入错误(“字符串”类型的参数不可分配给类型参数) - Typescript enums typing error (argument of type 'string' is not assignable to parameter of type) Typescript - 'string | 类型的参数 (string | null)[]' 不能分配给“string”类型的参数 - Typescript - Argument of type 'string | (string | null)[]' is not assignable to parameter of type 'string' 'string | 类型的参数号码 | 字符串[] | 将 JavaScript 转换为 TypeScript 时,undefined' 不可分配给“字符串”类型的参数 - Argument of type 'string | number | string[] | undefined' is not assignable to parameter of type 'string' when converting JavaScript to TypeScript 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' 获取:'string | 类型的参数 undefined' 不可分配给“RequestInfo”类型的参数 - Fetch : Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo' 输入'字符串| null' 不能分配给类型 'string | TypeScript 的未定义错误 - Type 'string | null' is not assignable to type 'string | undefined error with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM