首页 > 解决方案 > Typescript - React.FC 是任何而不是检查道具

问题描述

我创建了一个reactjs应用程序使用npx create-react-app myapp --template typescript

我创建的组件

import React from 'react';

interface IPerson {
   name: string;
   age: number;
}

const Person:React.FC<IPerson> = (props:IPerson) => {
    const {name, age} = props;

    return <div>{name} {age}</div>;
}

如果我没有通过所需的道具,那么打字稿也不会抛出任何错误。即使我运行应用程序,我也没有收到任何警告或错误。

 <Person name='xyz' />

上面的行应该抛出错误。但它工作正常。

我试图找到原因,但没有找到可以解决此问题的方法。我发现的只是当我将鼠标悬停在一个React.FC它显示的 typeany而不是 type 时IPerson在此处输入图像描述

如果有人可以对此有所了解,将不胜感激。

标签: reactjstypescriptreact-props

解决方案


当我npm install再次运行时,我注意到终端中存在以下错误:

gyp: No Xcode or CLT version detected!
gyp ERR! configure error

在此处输入图像描述

我做了以下步骤来解决这个问题:

xcode-select --print-path // this will give path of xcode-select install. use it to remove it;

sudo rm -r -f /Library/Developer/CommandLineTools

xcode-select --install // install it again

在我运行npm installVSCode 的上述步骤之后,如果缺少所需的道具,则开始抛出错误。


推荐阅读