Skip to content

createColumnHelper loses typesafety when TData contains only optional fields #5272

@jrozbicki

Description

@jrozbicki

Describe the bug

When TData type passed to createColumnHelper has all fields optional, we lose type-safety when using columnHelper.accessor function.

All fields are infering types just fine, but we lose this sanity check and this column becomes ColumnDef<Person, unknown>.

Marking any field as required (lifting ? from at least one field e.g. firstName: string;) will bring back expected error.

Not sure if that's a bug or a feature, but we actually spend some time before noticing typo.

Your minimal, reproducible example

Codesandbox forked Basic example

Steps to reproduce

  1. Fork Basic example
  2. Make all fields of Person optional:
type Person = {
  firstName?: string;
  lastName?: string;
  age?: number;
  visits?: number;
  status?: string;
  progress?: number;
};
  1. Use not existing key of Person:
const columnHelper = createColumnHelper<Person>();

const columns = [
  columnHelper.accessor("definitely-not-a-person-key", { // This doesn't throw any ts errors
    cell: (info) => info.getValue(),
    footer: (info) => info.column.id,
  }),
  ...

Expected behavior

Expected line 52. to throw ts error

Argument of type '"definitely-not-a-person-key"' is not assignable to parameter of type 'AccessorFn<Person> | "firstName" | "lastName" | "age" | "visits" | "status" | "progress"'

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Platform doesn't matter - ts issue

react-table version

^8.11.3

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions