如何在TypeScript的for-of循环中修复’:’ expected.ts(1005)错误?

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

How do I fix ':' expected.ts(1005) in a for-of loop in TypeScript?

问题

我正在做的是在我的React项目的CardContainer组件中读取位于public目录中的JSON文件,如下所示:

import { useAsync } from "react-async";

type ProjectData = {
    title: string,
    desc: string,
    website: string,
    repo: string,
    tags: Array<string>
}

type ProjectContainerData = {
    projects: Array<ProjectData>
}

async function getData() {
  let output: ProjectContainerData | undefined = undefined;
  await fetch("/myProjects.json")
    .then((response) => response.text())
    .then((data) => {
      output = JSON.parse(data);
    });
  return output;
}

function CardContainer() {
  const { data, error, isPending } = useAsync(getData);
    for (let project of data?.["projects"]) {
      // error is here
    }
  return <></>;
}

export default CardContainer;

当我编辑我的代码时,我看到了&#39;:&#39; expected.ts(1005)错误。当我尝试使用npm start编译它时,我看到了相同的错误:

ERROR in src/components/CardContainer.tsx:27:42
TS1005: &#39;:&#39; expected.
    25 | function CardContainer() {
    26 |   const { data, error, isPending } = useAsync(getData);
  &gt; 27 |     for (let project of data?.["projects"]) {
       |                                          ^
    28 |
    29 |     }
    30 |   return <></>;

我的TypeScript版本是4.9.5。是否有简单的方法来避免这个错误?

英文:

What I am doing is reading a JSON file located in the public directory of my React project in the CardContainer component, as can be seen below:

import { useAsync } from &quot;react-async&quot;;

type ProjectData = {
    title: string,
    desc: string,
    website: string,
    repo: string,
    tags: Array&lt;string&gt;
}

type ProjectContainerData = {
    projects: Array&lt;ProjectData&gt;
}

async function getData() {
  let output: ProjectContainerData | undefined = undefined;
  await fetch(&quot;/myProjects.json&quot;)
    .then((response) =&gt; response.text())
    .then((data) =&gt; {
      output = JSON.parse(data);
    });
  return output;
}

function CardContainer() {
  const { data, error, isPending } = useAsync(getData);
    for (let project of data?[&quot;projects&quot;]) {
      // error is here
    }
  return &lt;&gt;&lt;/&gt;;
}

export default CardContainer;

When I am editing my code, I see the &#39;:&#39; expected.ts(1005) error. When I try to compile it with npm start, I see the same thing:

ERROR in src/components/CardContainer.tsx:27:42
TS1005: &#39;:&#39; expected.
    25 | function CardContainer() {
    26 |   const { data, error, isPending } = useAsync(getData);
  &gt; 27 |     for (let project of data?[&quot;projects&quot;]) {
       |                                          ^
    28 |
    29 |     }
    30 |   return &lt;&gt;&lt;/&gt;;

My TypeScript version is 4.9.5. Is there any way to easily avoid this error?

答案1

得分: 1

?[...] 运算符不存在,正确的语法是 ?.[...]

因此 ?.[&#39;projects&#39;] 或者 ?.projects

参考链接:

英文:

?[...] operator does not exist, the right syntax for it is ?.[...]

Hence ?.[&#39;projects&#39;], or ?.projects

References:

答案2

得分: -1

更新:当我将这段代码更改为以下内容时,错误消失了:

const { data, error, isPending } = useAsync&lt;ProjectContainerData | any&gt;(getData);

以及当我将这段代码更改为以下内容时:

for (let project of data.projects) {
英文:

Update: The error goes away when I change this:

const { data, error, isPending } = useAsync(getData);

to this:

const { data, error, isPending } = useAsync&lt;ProjectContainerData | any&gt;(getData);

and when I change this:

for (let project of data?[&quot;projects&quot;]) {

to this:

for (let project of data.projects) {

huangapple
  • 本文由 发表于 2023年8月4日 08:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832283.html
匿名

发表评论

匿名网友

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

确定