JSMonk/hegel

Refined variable can't be returned

amatiasq opened this issue · 1 comments

I was wondering why the example code in Why do we need static analysis (Playground)

function deserializeUser(stringifiedUserJson) {
  const maybeUser = JSON.parse(stringifiedUserJson);
  if (
    typeof maybeUser === "object" &&
    maybeUser !== null &&
    "name" in maybeUser &&
    typeof maybeUser.name === "string"
  ) {
    return maybeUser;
  }
  throw new TypeError("Provided serialized user is invalid!");
}

The inferred type for deserializeUser was (string) => unknown throws SyntaxError | TypeError. I was expecting it to be (string) => { name: string, ... } throws SyntaxError | TypeError.

So I changed it to have a User type and found this bug...

Screenshot 2020-12-03 at 14 13 46

It says the type of maybeUser is { 'name': string, ... } and below the error:

Type UNKNOWN is incompatible with "{ 'name': string, ... }"

I mean the affirmation is right but maybeUser is not unknown here.

Playground

Fixed and will be available in the next Hegel version.