I have an error that makes no sense, I am typing the value of my state with hooks, but he says the error is not the same type.
Already tried with empty array and even array with some data and always error is the same.
import React, { useState } from 'react';
import { Row, Col } from 'config/styles';
import Bed from './Bed';
interface DataTypes {
date: string;
value: number;
}
function Beds(): JSX.Element {
const { data, setData } = useState<DataTypes[]>([]);
return (
<>
<Row>
{data.map((d, i) => (
<Col key={i} sm={16.666} lg={10}>
<Bed {...d} />
</Col>
))}
</Row>
</>
);
}
export default Beds;
Erro this:
TypeScript error in /Users/keven/Documents/carenet/orquestra-frontend/src/Beds/index.tsx(11,11):
Property 'data' does not exist on type '[DataTypes[], Dispatch<SetStateAction<DataTypes[]>>]'
to add the custom property into the HTMLAttributes interface. Then we can add the custom attribute into the HTML elements in our React project. To fix the "Property does not exist on type ‘DetailedHTMLProps, HTMLDivElement>’" error with React and TypeScript, we can extend the react module’s types using a declare statement.
... I think hooks are exciting. I also think that TypeScript’s great generics and type inference features are a perfect match to make your hooks type safe, without doing too much. That’s TypeScript’s greatest strength: Being as little invasive as possible, while getting the most out of it.
The error "Property does not exist on type 'never'" occurs when we forget to type an array or don't type the return value of the `useRef` hook. To solve the error, use a generic to explicitly type the state array or the ref value in your React application.
Our desire for strong typing is that values we initially set, get per component update, and set through events, always have the same type. With the provided typings, this works without any additional TypeScript: And that’s it. Your code works with out any extra type annotations, but still typechecks.
It should be an array, not an object:
const [data, setData] = useState<DataTypes[]>([]);
You have this indication in the error message:
type '[DataTypes[], Dispatch<SetStateAction<DataTypes[]>>]'
you should use const [ data, setData ] instead of const { data, setData }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With