microsoft / microsoft/TypeScript

Multiple keys being remapped to same key causes unstable modifier propagation

Aperta
#62,318 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

### 🔎 Search Terms

Mapped Types, Key Remapping

### 🕗 Version & Regression Information

- This has been happening since first release of key remapping at 4.1 (tested with v4.1.5 at Playground) until nightly (tested with v6.0.0-dev.20250822 at Playground).

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAShC2BDMBpCIAqB7AkgOwEtgDEAbABUQCdgAeDAPigF4oBvAKCm6gG0UoBPFADW6LADMoGKIgDOUARAAewCHgAmCgAYASNkIkQqUAGIEqc4AF8AdPsPGoAfThXr2qAH4zFq1AAuRQBdILwAV1JSAG4Oa1iOUEhfS2AAeTBiLDwyFlgEZDRMXEJiMkoaWk4eKAB6WqgAPS8uHgAiCSwsWwAjajavIKsqIQBzWJqOrt7EAC82sPD4HuNY6wYEpOhzVLgAR3CLCA08uCRUdGx8IhIKajpqnnqmlsnO7r75xeXV1u4pj79QZQYZjNYbDhAA

### 💻 Code

```ts
type RemapKeyToInitialPart = {
[K in keyof T as K extends `${infer First}.${infer _Rest}` ? First : K]: null;
};

type FirstOptional = RemapKeyToInitialPart<{
// ^?
"foo.bar"?: string;
"foo.baz": number;
}>;

type FirstRequired = RemapKeyToInitialPart<{
// ^?
"foo.baz": number;
"foo.bar"?: string;
}>;
```

### 🙁 Actual behavior

`type FirstOptional` and `type FirstRequired` results in different types:
- `type FirstOptional` has *optional* property `foo`
- `type FirstRequired` has *required* property `foo`

### 🙂 Expected behavior

`type FirstOptional` and `type FirstRequired` results in same type. I would expect it to be same as ~~`type FirstRequired`~~ `type FirstOptional`.

### Additional information about the issue

My motivation was to implement a utility `type Expand` which transforms `{ "foo.bar": number }` to `{ foo: { bar: number } }` ([playground](https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true#code/C4TwDgpgBAogHmAhgOwCYB4AqA+KBeKAbwCgoyoBtAaSgEtkoBrCEAewDMpMpEBnKAGK0ATr2AAFRMOAB5duirYAugC4oAJQhiZAIwBWEAMbAsAGkEixk6XIXZsxAL7FioSBdESps+YvxQaCDhgCDR+AAMAEkJ6dghhQUcAOmixYXoAc0dwqAB+QSg1GgAyKDTMlzdoTW19IxNMc0Dg0NR+cuQM3AJuIJCwokpxOgYqJVy1WPioADUoRzzZwtgEFAwSciGRphYOLh5+Yb7WiOiqZOiphJrgbMWb5eQIADd41S4KcSUAbicHYlQRgANlJoIZWMgxFBgABGNTwJBodAbcgAIgAFqwMhAkuwAK4ZRBJMC0NioiZQZB4gC2Oniv02GKxOPYrFYSR0UlRag6GV+jmwv1hSUx2NyuIJiHFJLYZUQwFovHYtC0lJpdISAB8oHi0BBlU9UK4YSLmbi2RypHKFUqVe1gOlOr8AcDQVBwZDgNCAEzw1ZIlFkJnY83sznCbllB2ZBlo0UsyXE0msclqKm0+l-IXe01iiWE6XJ62K5Wq9MaqDa3WAg0QI3AHPx0OWhK8eUlu1Rx184hAA))

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 i due esempi di tipi mappati usando il TypeScript Playground collegato e confronta FirstOptional con FirstRequired. Analizza il comportamento del compilatore nel rimappamento delle chiavi, quindi aggiungi un test di regressione che dimostri che entrambi gli ordini delle dichiarazioni producono la stessa opzionalità delle proprietà; il lavoro è completato quando il test passa con risultati identici.

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à
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.