JSMonk / hegel

An advanced static type checker

Home Page:https://hegel.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refined variable can't be returned

amatiasq opened this issue · 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.