microsoft / microsoft/TypeScript

Type argument with a subset of the parameter constraint's optional keys incorrectly reported as unassignable to constraint

Open
#63,725 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### 🔎 Search Terms

"does not satisfy the constraint" "keyof" "optional"

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "constraint"

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiAq4IB54BooFUoQB7AgDsATAZygG8oBtAaSgEtCoBrCEAewDMp4BdAPwAuKAFdCLQhwDuzAL4A+KAF4sAbgCwAKG0BjADYBDUuTgBhIydRKK2qFABuh-aIgBCEXESRUGKnUZmNk4eeCgAMihSYAAnJgBzQRFxSRl5BU0tOW0gA

### 💻 Code

```ts
type MyType = U;

class MyClass {
value!: MyType;
}
```

### 🙁 Actual behavior

The following error is reported:

```
Type '{ [K in keyof T & string]?: unknown; }' does not satisfy the constraint '{ [K in keyof T]?: unknown; }'.
Type 'keyof T' is not assignable to type 'keyof T & string'.
Type 'string | number | symbol' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T'.
Type 'keyof T' is not assignable to type 'string'.
Type 'string | number | symbol' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
```

### 🙂 Expected behavior

No error should be reported because due to the keys being optional, `{ [K in keyof T & string]?: unknown }` is a subtype of `{ [K in keyof T]?: unknown }` for all `T`.

### 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

Reproduce the diagnostic from the linked TypeScript Playground example, then trace generic constraint checking for mapped types with optional properties and intersected keyof keys. The work is done when the example compiles without an error while preserving correct constraint checking for related cases.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.