microsoft / microsoft/TypeScript

Type cast should try to remove readonly modifier

Open
#36,404 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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)

Playground Link: http://www.typescriptlang.org/play/index.html?target=99&ts=3.8.0-dev.20200123&ssl=1&ssc=1&pln=6&pc=1#code/BQbQDAugBAhgzlcECUsEDsCuBbARgUwCcQIBuAKFEjUUlXikPxgBMB7dAGwE8os8iJCgHphUAIIIALgAt8UfAA8ADjHRwAlhyhsAZrWgMAxhzhTKSGk1YceB+hhwFiZKKIWFCbQheoNr7Fy8SA6MzIF2-M5C5EA

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.