[英]Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532))
I get the following typescript error: 我收到以下打字稿错误:
const myFunction = (
param1: string | undefined,
param2: { someProp: string } | undefined
) => {
if (!param1 && !param2) {
return;
}
// Here I get the following Typescript error:
// (parameter) param2: { someProp: string } | undefined
// Object is possibly 'undefined'.ts(2532)
const param3 = param1 ? param1 : param2.someProp;
};
the following works: 以下作品:
const param4 = param1 ? param1 : param2 ? param2.someProp : null;
but seems redundant to check for null or undefined twice. 但是检查两次null或undefined似乎多余。
I have to mention that the strictNullChecks
option is set to true
in the compilerOptions and want to keep it like this. 我不得不提到的是, strictNullChecks
中的strictNullChecks
选项设置为true
,并希望保持这种状态。
Any idea why I get this error? 知道为什么我会收到此错误吗?
Here's a CodeSandbox with the code: https://codesandbox.io/s/jn2mp01q2v 这是一个带有代码的CodeSandbox: https ://codesandbox.io/s/jn2mp01q2v
The sad truth about the TypeScript compiler is that it just isn't as smart as a human being (as of TypeScript 3.4 anyway) and so its control flow analysis is but a pale shadow of the sort of analysis you can perform yourself. 关于TypeScript编译器的一个可悲的事实是,它不像人类那样聪明(无论如何从TypeScript 3.4开始),因此它的控制流分析只是您可以自己进行的那种分析的淡薄阴影。 Of course, it is very consistent about its analysis, whereas mine tends to get worse when I haven't eaten recently. 当然,它的分析非常一致,而当我最近没有进食时,我的情况会变得更糟。
If you perform a check on a variable of a union type which completely eliminates one or more of the constituents of that union, the compiler will happily narrow the type of the variable for you: 如果您对联合类型的变量执行检查,从而完全消除了该联合的一个或多个组成部分,则编译器将很高兴为您缩小变量的类型:
param1.charAt(0); // error, possibly undefined
if (!param1) return;
param1.charAt(0); // okay now
But one thing the compiler just doesn't do is keep track of correlated variables outside of discriminated unions . 但是编译器不会做的一件事情是跟踪区别联合之外的相关变量 。 What you've eliminated by checking 通过检查消除了什么
if (!param1 && !param2) return;
is the possibility that both param1
and param2
can be undefined
at the same time. 可能无法同时undefined
param1
和param2
。 You've taken two previously independent variables, and made them correlated to each other. 您已经采用了两个先前独立的变量,并使它们彼此相关。 Which the compiler doesn't keep track of. 编译器不跟踪的。 Since param1
and param2
can both still be undefined
(just not at the same time), the compiler treats them as still independent and you are left with your problem. 由于param1
和param2
仍然都不能undefined
(只是不能同时定义),因此编译器将它们视为仍独立的,您将面临问题。
You can do what the other answer suggested and use a type assertion , which is meant for occasions where you're smarter than the compiler and don't want to try to lead the compiler through the task of understanding what you already know: 您可以执行其他答案所建议的操作,并使用类型断言 ,这是在您比编译器更聪明并且不想尝试引导编译器完成了解您已经知道的任务的情况下使用的:
const param3 = param1 ? param1 : param2!.someProp; // I'm smarter than the compiler 🤓
Note that I've used the non-null assertion operator !
请注意,我已经使用了非null断言运算符 !
which is probably the most concise way possible to assert your superiority over the machine. 这可能是维护您在机器上的优势的最简洁方法。 Also beware that such assertions are not safe, since you might be mistaken about your superiority. 还要注意,这样的主张并不安全,因为您可能会误以为自己的优势。 So only do something like this after you double and triple check that there is no way for param2
to be undefined
. 因此,只有在您仔细检查了三次以确保没有undefined
param2
方式之后,才可以做这样的事情。
Another thing you can do is restructure your code so as to lead the compiler through an analysis that it can do. 您可以做的另一件事是重组代码,以便引导编译器进行可以执行的分析。
const param3 = param1 || (param2 ? param2.someProp : undefined);
if (!param3) return;
param3.charAt(0); // string
This will work for you and you are only checking each parameter once. 这将为您工作,并且您只需检查每个参数一次。 The variable param3
is of type string | undefined
变量param3
的类型为string | undefined
string | undefined
, and is only undefined
if both param1
and param2
are falsy. string | undefined
,并且只有在param1
和param2
都为假时才是undefined
。 Each step in that code completely eliminates union constituents for each variable, without leaving any correlated types lying around to confuse the compiler. 该代码中的每个步骤都完全消除了每个变量的并集构成,而不会留下任何相关的类型来混淆编译器。
Either solution should work for you. 两种解决方案都可以为您工作。 Hope that helps; 希望能有所帮助; good luck! 祝好运!
Because you have given two types to param2 one is object and other is undefined. 因为为param2提供了两种类型,一种是对象,另一种是未定义的。 You have to give as 你必须给
const param3 = param1 ? const param3 = param1吗? param1 : (param2 as any).someProp; param1:(param2任意).someProp;
then it must work. 那么它必须工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.