-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS2532 Compound null check, object cannot be undefined #35260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
TS, in general, doesn't do multi-variable narrowing where the narrowing of variable It can get very expensive and complicated to do so. Off topic, seeing logical operators used on non-boolean operands triggers me =x You should write something like, //Using non-boolean expressions as boolean expressions also triggers me
if (createdAt) {
return createdAt;
}
return version ? version.createdAt : null; |
Narrowing can't produce conditional forms like what would be needed to handle this |
For the record, the expression form Duplicate (pretty much) of #31603 |
I have a similar condition that has been plaguing my ability to refactor long conditions into local variables. For example, see this code in playground: export default function actOnDataOrUndefined({ data }: { data?: string[] }) {
// This is fine
const ok = data && data.length != 0 ? actOnData(data) : null;
// Refactor the condition to a local variable and typescript
// reports that the call to actOnData is invalid because data can be undefined
// when it clearly(?) cannot be, since hasData is true only when data is not undefined.
const hasData = data && data.length != 0;
const not_ok = hasData ? actOnData(data) : null;
}
function actOnData(data: string[]) {
return data.length;
} |
Hi, I may be a noob, but I was wondering if it would be possible to detect that For example, you could pre-process the code into: const stuff = (version?: string, createdAt?: string) => {
const localVariable : string | undefined = version || createdAt
if (!(localVariable)) {
return null;
}
return localVariable;
}; |
TypeScript Version: 2.4.1-dev.20191120 (nightly)
TS2532
Object is possibly undefined
Narrow
Code
Expected behavior:
version
should be narrowed as defined.Actual behavior:
version
still considered possibly undefined.Playground Link:
https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgGrQM7APYmQbwChlkEoI5IATAQTAC5kMwpQBzQgX0MIV2aZgArjBjIAvMgAUAN0w4QAfkbooWXABpS5ShFphlg1iDYBKCQD4CxZMDFSAhFLIVqdZAB8PyOWoWnzIhIScmEoPBAhABsogG4bbhtQoXDtVz13Lx95XAA6F119eM54oA
The text was updated successfully, but these errors were encountered: