Skip to content

[Feature Request] Built in to make a type non-nullable #1874

Closed
@willemneal

Description

@willemneal

Consider the following example:

function takesGeneric<T>(t: T): void {
  if (!isNullable<T>() || t != null){
      // @ts-ignore
      if  (isDefined(t.call)){
        t.call()
      }
    }
}

function callsGeneric<T>(t: T): void {
  if (t != null) {
    takesGeneric(t); // Would be nice to have a `notNull<T>` here
  }
}

class Tee {
  call(): void {
    let x: i8 = 0;
  }
}

let y: Tee | null = new Tee();

callsGeneric(y);

The type passed to takesGeneric is no longer nullable since it's guaranteed to not be null. However, the type passed doesn't reflect this. A quick fix to this could be to add a new builtin that converts a type | null to type. Or in branches where the type is non-null this happens automatically.

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