microsoft / microsoft/TypeScript

Intersection for same key read-only property maybe wrong

Open
#61,344 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Intersection Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

Intersection readonly property

### 🕗 Version & Regression Information

- This changed between versions 4.4.4 and 4.5.5 (But all versions have errors)

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/FAFwngDgpgBAgjAvDA3jA1gLhgOwK4C2ARlAE4wC+oksAQkqjKVAIYAmA9jgDZgbb5iZSsGAAKFBRgsAzvBgAyGLQCUAOnQMADAG5xk6XPpK46zcl2jw0GAFkW6KAHVSASxAsi3KAB4AKgB8DGgAtMzsXLwwANoA0jCuOBhQYBwAZjB+ALrYfnFZlHqiVjQwAMIM9o4u7p7ePrQBehJSsuVm2s0GbRVKqhqd+q1Giu0DFkVAA

### 💻 Code

```ts

type A = { k: number }
type B = { readonly k: number }

({} as A & B).k = 0;
({} as B & A).k = 0;

type MakeWritable = { -readonly [K in keyof T]: T[K] };

type C = MakeWritable;
({} as C).k = 0;
({} as C & B).k = 0;
({} as B & C).k = 0;

```

### 🙁 Actual behavior

command:
```
z:\webdl\test>tsc --target esnext --lib esnext test.ts
```
output:
```
test.ts:15:15 - error TS2540: Cannot assign to 'k' because it is a read-only property.

15 ({} as C & B).k = 0;
~

test.ts:16:15 - error TS2540: Cannot assign to 'k' because it is a read-only property.

16 ({} as B & C).k = 0;
~

Found 2 errors in the same file, starting at: test.ts:15
```

### 🙂 Expected behavior

no error.

### Additional information about the issue

_No response_

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 with the Playground link and the `test.ts` reproduction, then run the shown `tsc --target esnext --lib esnext test.ts` command. Trace TypeScript's checker handling of intersections and readonly properties; done means the two `C` intersection assignments compile without TS2540 while the reproduction remains covered.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.