Hello I am using React and typescript with coinAPI. I have a question how can I get type of data. Because now I use type unknown and I can not use the data inside my project. What to do?
const SearchForm: React.FC<funcProps> = (props) => {
const [assetName, setAssetName] = useState<string>("");
const onChangeInputHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
setAssetName(e.target.value);
};
const onSearchSubmitHandler = async () => {
const getData = async () => {
const response = await fetch(
`https://rest.coinapi.io/v1/assets/${assetName}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-CoinAPI-Key": "key",
},
}
);
const data = await response.json();
console.log(data);
props.setAsset(data);
};
getData();
};
CodePudding user response:
You cannot get type defenition from external source.
You should describe data structure before using it.
For example:
type Asset = {
price_usd: number;
data_end: string;
volume_1mth_usd: number;
data_orderbook_end: string;
data_start: string;
type_is_crypto: number;
data_quote_start: string;
asset_id: string;
volume_1hrs_usd: number;
data_quote_end: string;
data_orderbook_start: string;
data_trade_start: string;
name: string;
data_symbols_count: number;
data_trade_end: string;
volume_1day_usd: number;
};
and then:
const data: Asset = await response.json();