question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type 'string' is not assignable to type '"col1" Typescript error TS2322

See original GitHub issue

Describe the bug (required) Type ‘string’ is not assignable to type '“col1”

Provide an example via Codesandbox! (required) I tried to produce a typescript react-table example without luck. i am really sorry

Steps To Reproduce (required)

`    const data = React.useMemo(
        () => [
            {
                col1: 'Value 1',
            },
        ],
        []
    );
const columns = React.useMemo(
    () => [{
        Header: 'what',
        accessor: 'col1',
    }
    ],
    []
);


const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
} = useTable( {
    columns,
    data,
} )`

Expected behavior (Recommended) No typescript error

Screenshots image

Desktop (please complete the following information):

  • OS: Linux Mint
  • Browser Chrome
  • Version react-table 7.6.2 “@types/react-table”: “^7.0.25”,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15

github_iconTop GitHub Comments

19reactions
DeeSouzacommented, Dec 17, 2020

This worked for me:

interface IData {
    col1: string;
    col2: string;
}

const data = React.useMemo<IData[]>(
  () => [
    {
      col1: "Hello",
      col2: "World"
    },
    {
      col1: "react-table",
      col2: "rocks"
    },
    {
      col1: "whatever",
      col2: "you want"
    }
  ],
  []
);

const columns = React.useMemo<Column<IData>[]>(
  () => [
    {
      Header: "Column 1",
      accessor: "col1"
    },
    {
      Header: "Column 1",
      accessor: "col2"
    }
  ],
  []
);

const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow
} = useTable({ columns, data });
2reactions
processedbeetscommented, Dec 9, 2020

Well done man, thanks for that example. I can confirm that resolved things for me. Perhaps you might be able to add that to one of the examples in this repo as I think it’ll help others too.

For clarification, the differences that assisted me between your example and the original were:

  1. Adding a Data interface: interface Data { title: string; captured: string; priority: string; area: string; isService: string; }

  2. Adding a TableProps interface: interface TableProps<D extends object> { columns: Column<D>[]; data: D[]; }

  3. Changing the first line of the Table component to specify the type of the props and use of generics: function Table<D extends object>({ columns, data }: TableProps<D>) {

  4. Finally, changing the columns object instantiation by passing in the type when using memo: const columns = useMemo<Column<Data>[]>(

Thanks again @datner, saved me a lot of time! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Type 'string' is not assignable to type - Stack Overflow
I mean something like this let myFruit:Fruit = "Apple" let something:string = myFruit as string It is giving me error: Conversion of type...
Read more >
Type 'string' is not assignable to type '"col1" Typescript error ...
Describe the bug (required) Type 'string' is not assignable to type '"col1" Provide an example via Codesandbox!
Read more >
Type 'string' is not assignable to type in TypeScript | bobbyhadz
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something...
Read more >
Type 'string' is not assignable to type 'string[]' : r/typescript
Hi - I'm trying to create a component where one of the properties of the component is an array of strings. I've set...
Read more >
Type 'string' is not assignable to type 'never'.ts(2322)
I have attached code in VS code which helps to generate dummy data. the basic purpose of having pagination in anguar 6 project....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found