microsoft / microsoft/TypeScript
Type cast should try to remove readonly modifier
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
The compiler should try to use mutable versions of readonly tuples when couldn't cast readonly tuples to mutable arrays mainly for usability of as const. The performance impact of this would be a little because the condition when this check runs is limited.
TypeScript Version: 3.7.x-dev.20200123
Search Terms:
Code
([0] as [0]) as number[];
([0] as [0]) as readonly number[];
// As the expansion of [0] as const
([0] as readonly [0]) as number[]; // error
([0] as readonly [0]) as readonly number[];
Expected behavior:
pass
Actual behavior:
Conversion of type 'readonly [0]' to type 'number[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
The type 'readonly [0]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.(2352)
Related Issues:
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 reproducing the readonly tuple cast examples in the TypeScript Playground or with TypeScript 3.7.x-dev.20200123, then trace the compiler's insufficient-overlap and readonly assignability checks. Done means the readonly tuple cast to number[] passes as expected without requiring an intermediate unknown cast, while the existing safety checks remain intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100