-
-
Notifications
You must be signed in to change notification settings - Fork 669
Closed
Labels
Description
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
}```