Skip to content

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

Closed
oluwasayo opened this issue Nov 21, 2019 · 5 comments
Closed

TS2532 Compound null check, object cannot be undefined #35260

oluwasayo opened this issue Nov 21, 2019 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@oluwasayo
Copy link

TypeScript Version: 2.4.1-dev.20191120 (nightly)

TS2532
Object is possibly undefined
Narrow

Code

interface Version {
  createdAt: string
}

const stuff = (version?: Version, createdAt?: string) => {
  if (!(createdAt || version)) {
    return null;
  }

  return createdAt || version.createdAt;
};

Expected behavior:
version should be narrowed as defined.

Actual behavior:
version still considered possibly undefined.

(parameter) version: Version | undefined
Object is possibly 'undefined'.(2532)

Playground Link:
https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgGrQM7APYmQbwChlkEoI5IATAQTAC5kMwpQBzQgX0MIV2aZgArjBjIAvMgAUAN0w4QAfkbooWXABpS5ShFphlg1iDYBKCQD4CxZMDFSAhFLIVqdZAB8PyOWoWnzIhIScmEoPBAhABsogG4bbhtQoXDtVz13Lx95XAA6F119eM54oA

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Nov 21, 2019

TS, in general, doesn't do multi-variable narrowing where the narrowing of variable x also narrows variable y.

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;

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 22, 2019
@RyanCavanaugh
Copy link
Member

Narrowing can't produce conditional forms like what would be needed to handle this

@fatcerberus
Copy link

For the record, the expression form x || y doesn't prove anything about either x or y except that "at least one of them is truthy--but we don't know which." This is not enough information to narrow either one.

Duplicate (pretty much) of #31603

@fpintos
Copy link
Member

fpintos commented Mar 21, 2021

I have a similar condition that has been plaguing my ability to refactor long conditions into local variables.
This actually breaks when we apply a suggested refactoring.

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;
}

@sdandois
Copy link

Hi, I may be a noob, but I was wondering if it would be possible to detect that createdAt || version.createdAt is the same expression in both cases. Of course, it would only work if the compounds are primitive types like string or number.

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

6 participants