microsoft / microsoft/TypeScript

Synthesized optional properties don't satisfy `Record<` target under EOPT: false

Offen
#59,180 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔎 Search Terms

switch map array narrow

🕗 Version & Regression Information

I went back to TS 4 - still present

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBDAnmYcBiBLANjYUAKAhlIQLYBqhWArqgLxwDOMUGAdgOZwA+cb1pAEZ4ecACKFco5q04BtALoBuALAAodaEiw47XFABmhAMaoAkphx4APABU4oXGwAmjJi3YcAfHADe6uEC+MmAALjhbVTUguDBiMkYAfnCAJWBjaGdrGU8AGnRsfSISCipaLyiAX3VNcGh4PTwjUzgzACFCRgxjS30AZWAOUmA2GDsHECdXd1lvCam3C0KbWx9-aKCkFHCAcgNlqB2qmrUteoRkc168AaGRsftHEemczh8GJasoW+HRgFFwFBgIwuhA2HYfLx2p1utdvoNfg8KrVtPAtqgfvcAPIoEgwaBwBg7QguHaiHbQI4o86NQwmK4HTH-QHA0Hgx6TZ5uV5zdYxdHhJkwHF4STQKIxMBA5zdSTA8KffoI+4QxTHDSnfikApfAByITcfgCQQ6UDMbiJgmIAH0MIwdrljYEOgAvc2EuA7K0u2329TVDUjATiDC-Nm2S5GjbO4ger3EB1OuCuuPenb+k5nHTo1piUMjNkevlBDDOcI8iWbS7hPNhjBgiMoAB0pqqomLgVL5Y8nErgSwhGEWG7sz7F22IbrDcuLcILqqUXUGTYzAQJBXBmg2oYAAoZVOVwrawX62xFABKBVwoXWOH64aMd5rJNAmDUKBsKMxfnVz0k5yJtG35SsAMrGHKjDhPuJ5gowTakIQYB7vmK6noSz5Ad+QSMAA7hgMDGAAFnAyEHk26Lnl+WHUYE4GMKgx6odOzamuEHY0Rxr7vp+7EcXx45hJ6+xfIB-FicEwzhHeBqzmajCOph4lYXEJSQVRSliVaZplnA0FMWwTalkmGnfgGJkxGZGmWUpdEMSh4Yzq6bHGRpXEfup5k-hOewHKJnnfmwIRSQc97ArObryS5nkqQkzmKf5QTemYOl6WyhnOFF5nWVlmU0dleWZZU55JgGlRAA

💻 Code
export type FilterParamValue = string | number | Date | string[];

export interface IFilter<T extends string> {
    name: T;
    params?: Record<string, FilterParamValue>;
}

export interface IBasicFilterSegment<T extends string> extends IFilter<T> {
    type: 'filter';
}

export type IFilterSegment<T extends string> = IFilterSegmentExpression<T> | IBasicFilterSegment<T>;
export type SegmentOperator = 'and' | 'or';
export interface IFilterSegmentExpression<T extends string> {
    type: SegmentOperator;
    predicates: IFilterSegment<T>[];
}

enum FilterNames  {
    BarIs = 'bar_is',
    BazIs = 'baz_is'
}

enum DimensionType {
    Bar = 'bar',
    Baz = 'baz'
}

export type IDimension = {
    id: string;
    type: DimensionType.Bar;
} | {
    id: string;
    label: string;
    type: DimensionType.Baz;
};

const transform = (dimensions: IDimension[]): IFilterSegment<FilterNames> => {
    return {
        type: 'and',
        predicates: dimensions.map(dimension => {
            switch (dimension.type) {
                case DimensionType.Bar: {
                    return {
                        type: 'filter',
                        name: FilterNames.BarIs,
                        params: {
                            barId: dimension.id
                        }
                    }
                }
                case DimensionType.Baz: {
                    return {
                        type: 'filter',
                        name: FilterNames.BazIs,
                        params: {
                            bazId: dimension.id
                        }
                    }
                }
            }
        })
    }
}
🙁 Actual behavior

A compile-time error is displayed. It appears that the fact that we might return in the first switch case is "carried over" to the second one.

🙂 Expected behavior

Type narrowing to work correctly as the mapping is correct. In fact, if one extracts this mapping to a separate function, the behaviour is fixed. Playground.

Additional information about the issue

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere den gemeldeten Fehler mit dem bereitgestellten TypeScript Playground-Link und -Code und vergleiche EOPT: false mit der Variante mit extrahierter Funktion. Beginne damit, das Verhalten des Type-Checkers für den switch innerhalb von Array.map nachzuverfolgen; abgeschlossen ist die Aufgabe, wenn das Inline-Mapping ohne den Compile-Time-Fehler korrekt verengt und die Regressionstestabdeckung das Verhalten bestätigt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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