Fable
Fable copied to clipboard
[TS] Direct access to `Value` of a `Nullable` fails Typescript compilation
Hello,
let nullableTestWorking (s: Nullable<Test>) =
// Works because we have generate a type guard s != null
if s.HasValue then s.Value.Length else 0
let nullableTestFailing (s: Nullable<Test>) =
// Fails because we don't have a type guard s != null
s.Value.Length
export function nullableTestWorking(s: Nullable<Test>): int32 {
if (s != null) {
return value(s).Length | 0;
}
else {
return 0;
}
}
export function nullableTestFailing(s: Nullable<Test>): int32 {
return value(s).Length | 0;
}
This is failing because we don't have the safe guard when accessing s.Value.Length without passing by s.HasValue first. I think in this cases, we should force cast the type in Typescript, and if it fails at runtime this is not our fault this is what the user asked.
There are also cases where the code generate an intermediate variable:
let nullableTestFailing (s: Nullable<int>) =
// Fails because we don't have a type guard s != null
s.Value.ToString()
export function nullableTestFailing(s: Nullable<int32>): string {
let copyOfStruct: int32 = value(s);
return int32ToString(copyOfStruct);
}