microsoft / microsoft/TypeScript
Synthesized optional properties don't satisfy `Record<` target under EOPT: false
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported error using the provided TypeScript Playground link and code, comparing EOPT: false with the extracted-function variant. Start by tracing the type-checker behavior for the switch inside Array.map; done means the inline mapping narrows correctly without the compile-time error and regression coverage confirms the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100