How can I resolve TS7015 in my TypeScript/Next.js/React project when using an index expression that is not of type number?

huangapple go评论77阅读模式
英文:

How can I resolve TS7015 in my TypeScript/Next.js/React project when using an index expression that is not of type number?

问题

I Got Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015) error
我遇到了“Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)”错误。

I have Improted this file
我已经导入了这个文件。

import { useAmazon } from "@context/amazon";

I have Used hear
我在这里使用了。

const { data, current_product } = useAmazon();

and Got error
然后出现了错误。

const main_image = data[current_product].main_image; // error in current_product

@context/amazon file
@context/amazon 文件

import { JSObject } from "@classes/JSObject";
import React, { useState, useEffect, useContext, createContext } from "react";
import axios from "axios";
import { Product } from "types";

type AmazonContextType = {
  data: Product[] | null;
  current_product: string;
  setProduct: (product: string) => void;
  offers: JSObject | null;
  setOffers: (offer: JSObject | null) => void;
};

type Props = {
  children: React.ReactNode;
};

const amazonDefault: AmazonContextType = {
  data: null,
  current_product: '',
  setProduct: () => undefined,
  offers: null,
  setOffers: () => undefined,
};

const AmazonContext = createContext<AmazonContextType>(amazonDefault);

export function useAmazon() {
  return useContext(AmazonContext);
}

export function AmazonProvider({ children }: Props) {
  const [data, setData] = useState(amazonDefault.data);
  const [current_product, setProduct] = useState(amazonDefault.current_product);
  const [offers, setOffers] = useState(amazonDefault.offers);

  useEffect(() => {
    const fetchData = async () => {
      const url =
        "https://getdata.com/abc";
      const result = await axios(url);
      setData(result.data.payload);
    };
    fetchData();
  }, []);

  return (
    <AmazonContext.Provider
      value={{ data, current_product, setProduct, offers, setOffers }}
    >
      {children}
    </AmazonContext.Provider>
  );
}

How i can Solve this Error
我如何解决这个错误?

const main_image = typeof current_product === 'string' ? data[current_product as keyof typeof data].main_image : null;

I have also try chatGPT and google bard
我也尝试了 chatGPT 和 Google Bard。

Keywords:
关键词:
typescript
TypeScript
next.js
Next.js
reactjs
React.js
javascript
JavaScript
json
JSON

英文:

I Got Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015) error

I have Improted this file
import { useAmazon } from &quot;@context/amazon&quot;;

I have Used hear
const { data, current_product } = useAmazon();

and Got error
const main_image = data[current_product].main_image; // error in current_product

@context/amazon file

import { JSObject } from &quot;@classes/JSObject&quot;;
import React, { useState, useEffect, useContext, createContext } from &quot;react&quot;;
import axios from &quot;axios&quot;;
import { Product } from &quot;types&quot;;
type AmazonContextType = {
data: Product[] | null;
current_product: string;
setProduct: (product: string) =&gt; void;
offers: JSObject | null;
setOffers: (offer: JSObject | null) =&gt; void;
};
type Props = {
children: React.ReactNode;
};
const amazonDefault: AmazonContextType = {
data: null,
current_product: &#39;&#39;,
setProduct: () =&gt; undefined,
offers: null,
setOffers: () =&gt; undefined,
};
const AmazonContext = createContext&lt;AmazonContextType&gt;(amazonDefault);
export function useAmazon() {
return useContext(AmazonContext);
}
export function AmazonProvider({ children }: Props) {
const [data, setData] = useState(amazonDefault.data);
const [current_product, setProduct] = useState(amazonDefault.current_product);
const [offers, setOffers] = useState(amazonDefault.offers);
useEffect(() =&gt; {
const fetchData = async () =&gt; {
const url =
&quot;https://getdata.com/abc&quot;;
const result = await axios(url);
setData(result.data.payload);
};
fetchData();
}, []);
return (
&lt;AmazonContext.Provider
value={{ data, current_product, setProduct, offers, setOffers }}
&gt;
{children}
&lt;/AmazonContext.Provider&gt;
);
}

How i can Solve this Error

const main_image = typeof current_product === &#39;string&#39; ? data[current_product as keyof typeof data].main_image : null;
I have also try chatGPT and google bard

keyword
typescript
next.js
reactjs
javascript
json

答案1

得分: 0

你不能使用方括号语法通过对象的属性访问该项。
假设current_product是产品的name(可能是id,但我看不到Product类型),您需要执行以下操作:

const main_image = data !== null ? data.find(product => product === current_product).main_image : null;
英文:

You can't access the item by a property of the object using the square bracket syntax.
Assuming that current_product is the name of the product (might be id, but I can't see the Product type), you need to do the following:

const main_image = data !== null ? data.find(product =&gt; product === current_product).main_image : null;

huangapple
  • 本文由 发表于 2023年6月1日 21:01:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定