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.
I have to mention that the strictNullChecks
option is set to true
in the pilerOptions and want to keep it like this.
Any idea why I get this error?
Here's a CodeSandbox with the code:
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.
I have to mention that the strictNullChecks
option is set to true
in the pilerOptions and want to keep it like this.
Any idea why I get this error?
Here's a CodeSandbox with the code: https://codesandbox.io/s/jn2mp01q2v
Share Improve this question edited May 3, 2019 at 13:45 Sergiu asked May 3, 2019 at 11:58 SergiuSergiu 1,3961 gold badge18 silver badges33 bronze badges 2-
the following works ...
it doesn't work ifstrictNullChecks
is set totrue
– bugs Commented May 3, 2019 at 12:17 - @bugs sorry, my bad. You need to specify the correct type. Please take a look at the CodeSandbox link: codesandbox.io/s/jn2mp01q2v. – Sergiu Commented May 3, 2019 at 13:30
2 Answers
Reset to default 6The sad truth about the TypeScript piler 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. 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 pletely eliminates one or more of the constituents of that union, the piler 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 piler 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. You've taken two previously independent variables, and made them correlated to each other. Which the piler doesn't keep track of. Since param1
and param2
can both still be undefined
(just not at the same time), the piler treats them as still independent and you are left with your problem.
You can do what the other answer suggested and use a type assertion, which is meant for occasions where you're smarter than the piler and don't want to try to lead the piler through the task of understanding what you already know:
const param3 = param1 ? param1 : param2!.someProp; // I'm smarter than the piler