简体   繁体   English

绑定元素“xxx”在 Typescript 中隐式存在“任何”类型错误

[英]Binding element 'xxx' implicitly has an 'any' type error in Typescript

I have a ReactJS project based on TypeScript.我有一个基于 TypeScript 的 ReactJS 项目。 The following is one such file: Layout.tsx以下是一个这样的文件:Layout.tsx

import React from 'react';
import { Helmet } from 'react-helmet-async';
import NavigationBar from './navigation/NavigationBar';
import Footer from './footer/Footer';

const Layout = ({ title, description, children }) => {
     return (
          <>
               <Helmet>
                    <title>{title ? title + " - React Boilerplate" : "React.js Boilerplate"}</title>
                    <meta name="description" content={description || "React.js Boilerplate"} />
               </Helmet>
               <NavigationBar />
               <main className="container">
                    {children}
               </main>
               <Footer />
          </>
     );
}

export default Layout;

For this I am constantly getting: Binding element 'title' implicitly has an 'any' type.为此,我不断得到: Binding element 'title' implicitly has an 'any' type. I tried solutions for this online, but all of them are for Javascript and seems not to match with mine.我在网上尝试了解决方案,但所有这些都是针对 Javascript 的,似乎与我的不匹配。 Help is appreciated here.帮助在这里表示赞赏。

You are missing typing for your component (your function).您缺少为您的组件(您的功能)输入的内容。 First you have to tell TS that this is FunctionalComponent and second you have to tell it about it's props.首先你必须告诉 TS 这是FunctionalComponent ,其次你必须告诉它它的 props。

here is how you can do it这是你怎么做的

import React from 'react';
import { Helmet } from 'react-helmet-async';
import NavigationBar from './navigation/NavigationBar';
import Footer from './footer/Footer';

interface LayoutProps {
  title?: string;
  description?: string;
}

const Layout: React.FC<LayoutProps> = ({ title, description children }) => {
     return (
          <>
               <Helmet>
                    <title>{title ? title + " - React Boilerplate" : "React.js Boilerplate"}</title>
                    <meta name="description" content={description || "React.js Boilerplate"} />
               </Helmet>
               <NavigationBar />
               <main className="container">
                    {children}
               </main>
               <Footer />
          </>
     );
}

export default Layout;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM