-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
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).