microsoft / microsoft/TypeScript

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

Aperta
#63,725 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Mapped Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci la diagnostica dell’esempio collegato di TypeScript Playground, quindi segui il controllo dei vincoli generici per i tipi mappati con proprietà opzionali e intersected keyof keys. Il lavoro è completato quando l’esempio viene compilato senza errori, mantenendo al contempo il corretto controllo dei vincoli per i casi correlati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.