Type 'string' is not assignable to type '"col1" Typescript error TS2322
See original GitHub issueDescribe 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
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:
- Created 3 years ago
- Comments:15
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This worked for me:
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:
Adding a Data interface:
interface Data { title: string; captured: string; priority: string; area: string; isService: string; }
Adding a TableProps interface:
interface TableProps<D extends object> { columns: Column<D>[]; data: D[]; }
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>) {
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! 👍