Typescript样式组件错误:“键入'{子项:string; }'与类型' IntrinsicAttributes '没有共同属性,”

tyg4sfes  于 2023-06-24  发布在  TypeScript
关注(0)|答案(7)|浏览(151)

在样式化组件上获取类型脚本错误
类型“{ children:string; }'与类型' IntrinsicAttributes '没有共同的属性。ts(2559)

import React from 'react'

import { NotificationSuccess, NotificationError } from '../../styles'

interface IProps {
  error?: boolean;
  message: string;
}

export const Notification = (props: IProps) => {
  const Note = () => props.error ? NotificationError : NotificationSuccess;
  // Error happens on <Note>
  return (<Note>{props.message}</Note>);
}

和样式:

import styled from 'styled-components';

export const NotificationDiv = styled.div`
  z-index: 11;
  position: fixed;
  left: 50%;
  margin-left: -160px;
  top: 1rem;
  padding: 1.5rem;
  width: 320px;
  height: auto;
  text-align: center;
  color: ${props => props.theme.offWhite};
  border-radius: 5px;
  cursor: pointer;
`

export const NotificationSuccess = styled(NotificationDiv)`
  background: ${props => props.theme.green};
`

export const NotificationError = styled(NotificationDiv)`
  background: ${props => props.theme.red};
`

我在这里找到了这个答案,我确实将我的package.json升级为以下内容,但这仍然没有帮助:
为什么这个 Package 的样式化组件错误“与”没有共同的属性

"styled-components": "4.0.3",
"@types/styled-components": "4.0.3",
"babel-plugin-styled-components": "^1.10.0",

完整的package.json

{
  "name": "",
  "version": "2.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "next -p 7777",
    "build": "next build",
    "start": "next start -p 8000",
    "test": "NODE_ENV=test jest --watch --no-cache",
    "test-win": "SET NODE_ENV=test&& jest --watch"
  },
  "license": "ISC",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "@zeit/next-typescript": "^1.1.1",
    "axios": "^0.18.0",
    "decko": "^1.2.0",
    "downshift": "^2.2.3",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "graphql": "^14.0.2",
    "graphql-tag": "^2.9.2",
    "graphql-tools": "^4.0.0",
    "lodash.debounce": "^4.0.8",
    "next": "^7.0.2",
    "next-routes": "^1.4.2",
    "node-sass": "^4.11.0",
    "nprogress": "^0.2.0",
    "prop-types": "^15.6.2",
    "ramda": "^0.26.1",
    "react": "^16.7.0",
    "react-adopt": "^0.6.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-transition-group": "^2.5.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "styled-components": "4.0.3",
    "tslint": "^5.12.1",
    "tslint-react": "^3.6.0",
    "typescript": "^3.2.4",
    "waait": "^1.0.2"
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.3.0",
    "@babel/preset-typescript": "^7.1.0",
    "@types/enzyme": "^3.1.15",
    "@types/jest": "^23.3.13",
    "@types/next": "^7.0.6",
    "@types/ramda": "^0.25.49",
    "@types/react": "^16.7.20",
    "@types/react-dom": "^16.0.11",
    "@types/react-redux": "^7.0.1",
    "@types/styled-components": "4.0.3",
    "@types/zeit__next-typescript": "^0.1.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^24.1.0",
    "babel-plugin-sass-vars": "^0.2.1",
    "babel-plugin-styled-components": "^1.10.0",
    "casual": "^1.5.19",
    "enzyme-to-json": "^3.3.4",
    "jest": "^24.1.0"
  },
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest.setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/.next/",
      "<rootDir>/node_modules/"
    ],
    "transform": {
      ".*": "babel-jest",
      "^.+\\.js?$": "babel-jest",
      "^.+\\.ts?$": "babel-jest",
      "^.+\\.tsx?$": "babel-jest"
    },
    "moduleFileExtensions": [
      "js",
      "json",
      "ts",
      "tsx"
    ],
    "modulePaths": [
      "<rootDir>/components/",
      "<rootDir>/pages/",
      "<rootDir>/shared/"
    ]
  }
}
yrefmtwq

yrefmtwq1#

如果创建零部件:

<DeliverNow>

</DeliverNow>

它自动接收props。当您声明它时:

const DeliverNow = () => {}

TypeScript会告诉你它们不匹配,因为实际上,DeliverNow接收props
实际上应该是:

const DeliverNow = (props:any) => {}
du7egjpx

du7egjpx2#

<Note>{props.message}</Note>Note({ children: props.message })相同,所以typescript抱怨函数Note不带任何参数,函数类型不匹配。它与样式化组件无关。
IntrinsicAttributes可能是编写功能组件时扩展的默认接口。或类似的东西idk XD)
我最好的猜测是你想要的是const Note = props.error ? NotificationError : NotificationSuccess;而不是你写的东西。
PS.我可能错了,但我基本上肯定这是事实。

oymdgrw7

oymdgrw73#

我得到了这个错误:
类型“{children:string;}'与类型' IntrinsicAttributes'没有共同的属性。ts
在我的react应用程序中动态添加标签时。我发现了一个很好的解决方案,它与typescript或样式化组件无关。
通过React.createElement创建节点就足够了
例如:

const Title: React.SFC<TitleProps> = ({ tag, styled, children }) =>
  React.createElement(tag, { ...styled }, children);

const TitleStyled = styled(Title)`Your styled`
gpnt7bae

gpnt7bae4#

这就是我的解决之道。我试图将我自己的组件命名为与依赖组件相同的名称,VS代码自动从依赖组件导入,而不是我的组件。

hivapdat

hivapdat5#

正如您可以阅读组件文档:

/**
 * A general `Component` has no implicit `children` prop.  If desired, you can
 * specify one as in `Component<{name: String, children: JSX.Element>}`.
 */
export declare type Component<P = {}> = (props: P) => JSX.Element;

因此,为了摆脱这个警告,您必须传递一个泛型,其中包括子项或任何您想要的 prop 。正确的实现是:

export const Notification: Component<IProps> = (props) => {
  const Note = () => props.error ? NotificationError : NotificationSuccess;
  // Error happens on <Note>
  return (<Note>{props.message}</Note>);
}

您不需要声明props类型,因为它是Component<P>的固有类型。

8cdiaqws

8cdiaqws6#

最常见的错误是当你将样式组件命名为与函数组件相同的名称时。只要给你的样式元素给予一些后缀,比如ButtonStyled或者ButtonContainer,你就是g2g

vngu2lb8

vngu2lb87#

使用函数表达式代替函数声明。EX:

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  )
}

相关问题