microsoft / microsoft/TypeScript
Synthesized optional properties don't satisfy `Record<` target under EOPT: false
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
switch map array narrow
🕗 Version & Regression Information
I went back to TS 4 - still present
⏯ Playground Link
💻 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’errore segnalato usando il link e il codice TypeScript Playground forniti, confrontando EOPT: false con la variante con la funzione estratta. Inizia tracciando il comportamento del type-checker per lo switch all’interno di Array.map; il lavoro è completo quando il mapping inline viene ristretto correttamente senza l’errore di compilazione e la copertura di regressione conferma il comportamento.
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
- 35/100