Skip to content

[Type Error] Null | Type must be cast after a conditional return #456

Closed
@willemneal

Description

@willemneal

Here is the working example: https://webassembly.studio/?f=zi21djvj57

Consider this example

function orNull(i: i32): Foo | null {
  if (i % 2 == 0)
  return null;
  return new Foo();
}

function example(): Foo | null {
  let x: Foo | null = orNull(1);
  if (x == null){
    return null;
  }
  return id(x); // x is not null, but compiler requires an explicit cast.
}

function id(f: Foo): Foo{
  return f;
}

Not sure how big of a problem this is since it might be better to enforce being explicit, but I thought I'd share it. Also the same is true for if/else branches.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions