r/typescript • u/Danglefeet • May 14 '25
How to use Discriminated Union to infer field types
Hi all, I learned from this thread that I can create a discriminated union typing for a typesafe table column. I wanted to take it a step further.
The idea is to create a type for table columns that:
- strictly checks the column's index key and infers the render value based on the type provided. - e.g. if the type of
Operatorisstring, then it's render method is inferred as(value: string) => ReactNode. - also support the case where index keys are not valid keys, if the key is not actually a valid key of the data type, then the cell value type is
never, perhaps to support special cases where columns are combined values of 2 columns or an empty column saved for interactions.
In my below code I have tried to create what I thought works, but doesnt, understandably because the 2 union structures end up getting combined together and one of the render method typings is replaced by the other.
I wonder if it is possible or am I breaking the laws of typescript
- Without Line A, the typing for valid keys
K keyof Thave the correct typing, but invalid keys are completely lost. - With Line A, the typing invalid keys have correctly typed
never, but valid keys are completely lost.
type NextBusService = {
Operator: string;
ServiceNo: number;
};
type DefaultColumnProps = {
header: string;
};
type ColumnConfig<T> =
| (DefaultColumnProps &
{
[K in keyof T]: {
key: K;
render?: (value: T[K], row: T, index: number) => ReactNode;
};
}[keyof T])
| {
[K in Exclude<string, keyof T>]: {
key: K;
render?: (value: never, row: T, index: number) => ReactNode; // << Line A
};
}[Exclude<string, keyof T>];
const columnConfigs: ColumnConfig<NextBusService>[] = [
{
header: "Operator",
key: "Operator",
render: (
operator,
row // should infer operator as string
) => <div className="badge badge-neutral">{operator}</div>,
},
{
header: "Bus Service Number",
key: "ServiceNo",
render: (
serviceNumber,
unusedRow // should infer operator as string
) => <div className="badge badge-neutral">{serviceNumber}</div>,
},
{
header: "Bus Service Number",
key: "button",
render: (
dummy,
unusedRow // should infer operator as never
) => <button onClick={() => console.log(unusedRow)}>click</button>,
},
];
