Open
Description
TypeScript Version: 2.1.6
Code
interface StringOnly {
val: string;
}
interface StringOrNull {
val: string | null;
}
const obj: StringOnly = { val: "str" };
// should not be allowed
const nullable: StringOrNull = obj;
nullable.val = null;
// obj is now in a bad state
Expected behavior:
The sample should not compile, as it's not safe. Type casts/assertions should be required to override type incompatibility errors here. (Or, of course, cloning the object itself const nullable: StringOrNull = { ...str };
).
For comparison, Flow does not allow the sample code.
Actual behavior:
The sample compiles and further accesses of str.val
will likely result in exceptions.
EDIT: Removed all references to readonly
, as discussion of that modifier is overshadowing the issue.