[英]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:有三种方法可以解决这个问题:
if (req.apiGateway === undefined) { throw new badRequestException(); }
if (req.apiGateway === undefined) { throw new badRequestException(); }
if (req.apiGateway === undefined) { throw new badRequestException(); }
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.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.?.
?.
to the rest of the line: req.apiGateway?.event?.headers
.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.userid
是string|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.