打字稿样式组件错误:“类型'孩子:字符串;'与类型'IntrinsicAttributes'没有共同的属性。”

Posted

技术标签:

【中文标题】打字稿样式组件错误:“类型\'孩子:字符串;\'与类型\'IntrinsicAttributes\'没有共同的属性。”【英文标题】:Typescript styled-component error: "Type ' children: string; ' has no properties in common with type 'IntrinsicAttributes'."打字稿样式组件错误:“类型'孩子:字符串;'与类型'IntrinsicAttributes'没有共同的属性。” 【发布时间】:2019-08-03 10:11:52 【问题描述】:

在我的样式组件上遇到打字错误

类型'孩子:字符串; ' 与类型 '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 升级到了以下内容,但这仍然没有帮助:

Why this wrapped styled-component errors "has no properties in common with"

"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/"
    ]
  


【问题讨论】:

您是否尝试删除node_modules 并重新安装? 请考虑将函数组件定义为基于接口的泛型类型。例如常量通知:React.FC 【参考方案1】:

好的,就像上面所说的,只是为了澄清你是否创建了一个组件

   <DeliverNow>

        </DeliverNow>

它会自动将道具传递给它,如果你声明它

const DeliverNow = () => 

Typescript 会告诉你它们不匹配,因为实际上,DeliverNow 接受了一些道具

所以在实际意义上,它的意思是

const DeliverNow = (props:any) => 

【讨论】:

【参考方案2】:

&lt;Note&gt;props.message&lt;/Note&gt;Note( children: props.message ) 相同,因此打字稿抱怨函数 Note 不接受任何参数并且函数类型不匹配。它与样式化组件无关。

IntrinsicAttributes 可能是您编写功能组件时扩展的默认接口。或者类似 idk xD)

我最好的猜测是你想要const Note = props.error ? NotificationError : NotificationSuccess; 而不是你写的。

附言。我可能是错的,但我很确定是这样的。

【讨论】:

你说得对,是的,我不需要 = () =&gt; 部分。【参考方案3】:

我收到了这个错误:

类型'孩子:字符串; ' 与类型 'IntrinsicAttributes'.ts 没有共同的属性

在我的反应应用程序中动态添加标签时。 我找到了一个很好的解决方案,它与打字稿或样式组件无关。

通过React.createElement创建节点就够了

例如:

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

const TitleStyled = styled(Title)`Your styled`

【讨论】:

【参考方案4】:

出现该错误的最常见情况是当您将样式组件命名为与函数组件相同的名称时。只需给您的样式组件添加一些后缀,例如 ButtonStyledButtonContainer,您就是 g2g

【讨论】:

以上是关于打字稿样式组件错误:“类型'孩子:字符串;'与类型'IntrinsicAttributes'没有共同的属性。”的主要内容,如果未能解决你的问题,请参考以下文章

打字稿,样式组件错误:TS2769:没有重载匹配此调用。重载 1 of 2

Material-UI 组件的 styled-components 打字稿错误

样式组件 + 打字稿:“as”不可分配给类型 IntrinsicAttributes

React hook useRef 不适用于样式化组件和打字稿

自定义主题样式组件的打字稿

如何使用打字稿在可重用组件中导入html样式属性?