microsoft / microsoft/TypeScript
Unsafe type-incompatible assignments should not be allowed
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by compiling the provided TypeScript sample and tracing the type-checker entry point that accepts the assignment from StringOnly to StringOrNull. Compare the resulting assignability rules with the stated unsafe mutation, and consider existing discussion before defining what behavior and regression coverage would count as done.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100