Flow: Property is missing in null or undefined

Created on 18 Jan 2019  路  4Comments  路  Source: facebook/flow

function updateLocalPackageTemplate(
  newPackage: ?LocalPackageType,
  packages: Array<LocalPackageType> = PACKAGE_TEMPLATES
) {
  if (!newPackage || !newPackage.custom) { return packages; }

  const newPackages: Array<LocalPackageType> = [...packages];
  const index = newPackages.findIndex(
    (singlePackage: LocalPackageType) => singlePackage.name === newPackage.templatedFrom
  );

  newPackages.splice(index, 1, newPackage);

  return newPackages;
}

I get Cannot get `newPackage.templatedFrom` because property `templatedFrom` is missing in null or undefined [1].

I have an explicit truthy check on this so why is it complaining? Can't work it out.

question

All 4 comments

The call to findIndex clears the refinement. Can you do const templatedFrom = newPackage.templatedFrom; before the call?

If this fixes your problem please close this issue.

I saw that function calls clear refinements but I assumed the truthy check before would prevent that being an issue?

That's precisely the problem :) The truthy check happens _before_ the function call, so Flow "forgets" that information after the function call.

Flow can't prove that findIndex doesn't change newPackage.tempaltedFrom before calling that callback, so it conservatively assumes it does and errors.

Makes sense, thanks for your help.

Was this page helpful?
0 / 5 - 0 ratings