#Generic type infering doesn't work

12 messages · Page 1 of 1 (latest)

proven crypt
#

I have the type TableColumn and it has 3 generic arguments and I want the last 2 to be inferred by the valueAccessorFn and displayAccessorFn functions. for some reason the 2 arguments stay as "unknown".
help?

  value: TProperty;
  rowValues: TData;
  error?: string;
  onChange: (value: TProperty) => void;
  onBlur: () => void;
}

interface RenderCellValueProps<TProperty> {
  value: TProperty;
  onClick: () => void;
}


export interface TableColumn<TData, TDisplayProp = unknown, TValueProp = unknown> {
  name: string;
  header: string;
  valueAccessorFn: (value: TData) => TValueProp;
  displayAccessorFn: (value: TData) => TDisplayProp;
  renderEditableField?: (props: RenderEditableFieldProps<TData, TValueProp>) => ReactNode;
  renderCellValue?: (props: RenderCellValueProps<TDisplayProp>) => ReactNode;
  renderColumnValue?: (columnName: string) => void;
}


type Foo = {
    name: string
    details: {
        id: number
        str: string
    }
}

const column: TableColumn<Foo> = {
    displayAccessorFn: (value) => value.details.str,
    valueAccessorFn: (value) => value.details.str
}
green stag
#

@proven crypt TS does not do partial inference of arguments.

proven crypt
#

elab?

green stag
#

Either all generics are inferred or all are specified.

#

And inferring is really only for functions

proven crypt
#

I understand

green stag
#

When you do = unknown that means "if not passed, this will be unknown". It doesn't infer.

#

The usual pattern is to split the bits that need to be specified from the bits that are meant to be inferred.

#
function createColumnBuilder<TData>() {
    return function defineColumn<TDisplayProp, TValueProp>(col: TableColumn<TData, TDisplayProp, TValueProp) {
        return col;
    }
}
#
const columnBuilder = createColumnBuilder<Foo>();
const column = columnBuilder({
    displayAccessorFn: (value) => value.details.str,
    valueAccessorFn: (value) => value.details.str
});
proven crypt
#

interesting

#

thank you!