Reactjs 类型';字符串';没有与类型';相同的属性;物业<;文本、字符串和;{}>;
我有一个函数react元素,看起来像这样Reactjs 类型';字符串';没有与类型';相同的属性;物业<;文本、字符串和;{}>;,reactjs,typescript,Reactjs,Typescript,我有一个函数react元素,看起来像这样 import { CSSProperties } from 'styled-components' export interface StylesDictionary { [Key: string]: CSSProperties } interface Props { onClick: () => void buttonStyle?: StylesDictionary } export default function PreviewB
import { CSSProperties } from 'styled-components'
export interface StylesDictionary {
[Key: string]: CSSProperties
}
interface Props {
onClick: () => void
buttonStyle?: StylesDictionary
}
export default function PreviewButton({ onClick, buttonStyle }: Props) {
<PreviewButton
buttonStyle={{ marginLeft: '20px' }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
当我试图通过这样的风格
import { CSSProperties } from 'styled-components'
export interface StylesDictionary {
[Key: string]: CSSProperties
}
interface Props {
onClick: () => void
buttonStyle?: StylesDictionary
}
export default function PreviewButton({ onClick, buttonStyle }: Props) {
<PreviewButton
buttonStyle={{ marginLeft: '20px' }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
onClickPreview(api,tableDefinition)}
/>
Typescript给出了以下错误
Type 'string' has no properties in common with type 'Properties<ReactText, string & {}>'
类型“string”与类型“properties”没有共同的属性
你知道我可能做错了什么吗?那是因为你对
StylesDictionary
的类型定义不正确:如果你现在这样定义StylesDictionary
:
export interface StylesDictionary {
[Key: string]: CSSProperties
}
然后在您的PreviewButton
props上,buttonStyle
应该是:
<PreviewButton
buttonStyle={{ anyValue: {marginLeft: '20px'} }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
然后,您将能够按照您的意愿编写按钮样式
:
<PreviewButton
buttonStyle={{ marginLeft: '20px' }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
onClickPreview(api,tableDefinition)}
/>
这是因为您对StylesDictionary
的类型定义不正确:如果您现在这样定义StylesDictionary
:
export interface StylesDictionary {
[Key: string]: CSSProperties
}
然后在您的PreviewButton
props上,buttonStyle
应该是:
<PreviewButton
buttonStyle={{ anyValue: {marginLeft: '20px'} }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
然后,您将能够按照您的意愿编写按钮样式
:
<PreviewButton
buttonStyle={{ marginLeft: '20px' }}
onClick={() => onClickPreview(api, tableDefinition)}
/>
onClickPreview(api,tableDefinition)}
/>