microsoft / microsoft/TypeScript

Intersection with a mapped type breaks bypassing private assignability.

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

Nobody has claimed this yet.

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

Description

### 🔎 Search Terms

private assignability intersection mapped type

### 🕗 Version & Regression Information

Latest as of today.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEkoGdnwBrwN4CgCQADjAJYBuUALglAFzwB2ArgLYBGIMA3Pq3chSXoBzbgF8cOCgE8CCAJrwAvNni0GLdjHij4AMhW94-QUO3dps+AC0l8AArEwAawA8WVXSZsO2gDTwAcigAgD49Az4BYmEzCVAkOHgIEAp4AA86dG546ETk1Kk6OWzwXIR8+AAvOituHDTbKVU0BX0s+ttK5utw9qA

### 💻 Code

Given
```ts
declare class X {
private a: number;
b: string;
}
```

You're allowed to do:
```ts
type Y = { a: number } & { b: string };
declare let y: Y;
let x: X = y as Y & X;
```

But fails with:
```ts
type Z = Pick<{ a: number }, 'a'> & { b: string };
declare let z: Z;
let x: X = z as Z & X;
```

### 🙁 Actual behavior

`X` bypasses private assignability, but the _identical_ type `Y` errors.

### 🙂 Expected behavior

Either neither type is allowed to bypass private assignability, or both types are, since they're identical.

### 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 linked TypeScript Playground reproduction and compare assignability for the explicit intersection and the Pick-based intersection. Trace the compiler's private-member assignability handling and mapped-type intersection behavior, then verify that the two equivalent examples produce consistent results.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.