Skip to content

postfix ! causes runtime exception when assigning nullable variable to non-nullable variable #1095

@whoami-zhangster

Description

@whoami-zhangster

postfix ! is causing an issue with assigning a nullable string (postfixed with !) to a non-nullable string.

  • Functions with intermediary variable ( fn working() )
  • Does not function with direct assignment ( fn nonWorking() )
  • Functions with direct assignment if it is only one character ( fn onlyWorkingWithOneChar() )

Examples below:

class Class1 {
 id: string | null; 
 constructor() {
   this.id = null; // have also tried initializing to empty string
 }
}
class Class2 {
  id: string;
  constructor() {
    this.id = ""
  }
}

export function working(): string {
  let c1 = new Class1()
  let c2 = new Class2()

  c1.id = "test"

  // this works
  let temp = (c1.id!=null && c1.id!="")?c1.id!:"Is Null"
  c2.id = temp

  return c2.id
}

export function nonWorking(): string {
  let c1 = new Class1()
  let c2 = new Class2()

  c1.id = "test"
  

  /* This throws 
  RuntimeError: memory access out of bounds
  */
  c2.id = (c1.id!=null && c1.id!="")?c1.id!:"Is Null"

  return c2.id
}

export function onlyWorkingWithOneChar(): string {
  let c1 = new Class1()
  let c2 = new Class2()

  c1.id = "t"

  /* 
  However, this functions as expected. 
  */
  c2.id = (c1.id!=null && c1.id!="")?c1.id!:"Is Null"

  return c2.id
}``` 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions