Skip to content

Type inference with map & (otherMap | otherMap2) fails #37231

@marcesengel

Description

@marcesengel

TypeScript Version: Nightly

Search Terms: None (I couldn't come up with any, please excuse me)

Code

type Entity = {
  someDate: Date | null;
} & ({ id: string; } | { id: number; })

type RowRendererMeta<TInput extends {}> = {
  [key in keyof TInput]: { key: key; caption: string; formatter?: (value: TInput[key]) => string; };
}
type RowRenderer<TInput extends {}> = RowRendererMeta<TInput>[keyof RowRendererMeta<TInput>];

const test: RowRenderer<Entity> = {
  key: 'someDate',
  caption: 'My Date',
  formatter: (value) => value ? value.toString() : '-' // value: any
}

const thisIsNotTheIssue: Partial<RowRendererMeta<Entity>> = {
  someDate: {
    key: 'someDate',
    caption: 'My Date',
    formatter: (value) => value ? value.toString() : '-' // value: Date | null
  }
}

Expected behavior:
TypeScript inferes the argument of formatter to be Date | null.

Actual behavior:
When removing the & ({ id: string; } | { id: number; }) part of the Entity type definition, everything works as expected. However including it breaks type inference, resulting in an implicit any error/warning (depending on configuration), though it does not break the RowRendererMeta<...> type, as shown in the second const declaration.

Playground Link: Playground Link

Related Issues: None found.

Edit: Inference for id sadly does not seem to work at all.

Edit 2: Even though TS seems not to infer the type of the function argument, it complains when you try to use an invalid type (in this example string to name one).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions