'Typescript: Enforce that generic T has some property if another prop is present

I have a Table component with props like this:

type TableProps<T extends object> = {
  columns: Column<T>[];
  data: T[];
  expandable?: boolean;
};

I'd like to make it so that if the expandable prop is provided/set to true, it enforces that T extends {details: ReactNode} instead of T extends object, as the details node is what I want to render when expanding a row.

Is it possible to use generics in this way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source