Strange behavior of types while using nested mapped types and generics

Offen
#54,775 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
typescript
Bereich
compilers

Rechercherichtung

Beginne mit dem bereitgestellten TypeScript Playground-Link und dem Beispiel für den Builder eines verschachtelten Objekts und vergleiche den inferierten Typ von test mit den eigenständigen Deklarationen von n und p. Verfolge, wie verschachtelte generische Aufrufe OptionalyProped-Typen inferieren; fertig ist die Aufgabe, wenn die verschachtelten n-Eigenschaften durchgängig OptionalyProped<number> inferieren und die p-Eigenschaften OptionalyProped<21> beibehalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Needs Investigation

Bug Report

I got two differents typings while nesting objects using some object-builder functions and generic optional typings, keeping it quite basic.

These two typings show up in a one-in-two pattern as we dig into the nested objects.
The second one matches with the type of the object-builder.

It can be a misuse of mine so let me know ...

🔎 Search Terms
  • mapped type
  • nested
  • partial optional
  • generic
🕗 Version & Regression Information

Typescript versions 5.1.4 & 5.2.0-dev.20230625 & 4.1.6

Just discovered it, so AFAIK this is not a regression.

⏯ Playground Link

Just here

💻 Code
type OptionalyProped<OPTL extends number> = { optlValue?: OPTL }
export const optionalyProped = <OPTL extends number>(proped: OptionalyProped<OPTL>): OptionalyProped<OPTL> => proped


type ObjectOfProped<T extends { [key: string]: ObjectOrProped }> = { structure: T }
export const objectOfProped = <OBJ extends { [key: string]: ObjectOrProped }>(
    objectOf: ObjectOfProped<OBJ>
    ): ObjectOfProped<OBJ> => objectOf

type ObjectOrProped = ObjectOfProped<any> | OptionalyProped<any>

const test = objectOfProped({
	structure: {
		n: optionalyProped({}),
                p: optionalyProped({ optlValue: 21}),
		o: objectOfProped({
			structure: {
				n: optionalyProped({}),
                                p: optionalyProped({ optlValue: 21}),
				o: objectOfProped({
					structure: {
						n: optionalyProped({}),
                                                p: optionalyProped({ optlValue: 21}),
						o: objectOfProped({
							structure: {
								n: optionalyProped({}),
                                                                p: optionalyProped({ optlValue: 21}),
							},
						}),
					},
				}),
			},
		}),
	},
})

const n = optionalyProped({ }) // typed: OptionalyProped<number>
const p = optionalyProped({ optlValue: 21 }) // typed: OptionalyProped<21>

🙁 Actual behavior
typeof test = ObjectOfProped<{
    n: OptionalyProped<any>;                // first behavior
    p: OptionalyProped<number>;
    o: ObjectOfProped<{
        n: OptionalyProped<number>;         // second behavior (legit one)
        p: OptionalyProped<21>;
        o: ObjectOfProped<{
            n: OptionalyProped<any>;
            p: OptionalyProped<number>;
            o: ObjectOfProped<{
                n: OptionalyProped<number>;
                p: OptionalyProped<21>;
            }>;
        }>;
    }>;
}>
🙂 Expected behavior
typeof test = ObjectOfProped<{
    n: OptionalyProped<number>;             // always
    p: OptionalyProped<21>;
    o: ObjectOfProped<{
        n: OptionalyProped<number>;         // the same
        p: OptionalyProped<21>;
        o: ObjectOfProped<{
            n: OptionalyProped<number>;     // type
            p: OptionalyProped<21>;
            o: ObjectOfProped<{
                n: OptionalyProped<number>;
                p: OptionalyProped<21>;
            }>;
        }>;
    }>;
}>

Thanks

Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/TypeScript

Alle Issues in microsoft/TypeScript

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.