redux 类型脚本错误:没有与此调用匹配的重载[已关闭]

fykwrbwg  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(135)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
4天前关闭。
第一个

w51jfk4q

w51jfk4q1#

错误很明显,你不能把undefined赋给noMargin

Types of property 'noMargin' are incompatible.
  Type 'boolean | undefined' is not assignable to type 'boolean'.
    Type 'undefined' is not assignable to type 'boolean'

最简单的解决方法是使用双重否定运算符!!

<StyledButton noMargin={!!noMargin} {/* ...other properties*/} />

或者,您可以修复styledComponent以接受未定义的

const StyledButton = styled(Button)<{ elem: string, noMargin: boolean | undefined }>`

// this also works but has its drawbacks depending on what you want to do
// const StyledButton = styled(Button)<{ elem: string, noMargin?: boolean }>

使用?:而不是| undefined的原因取决于是否需要使用noMargin。https://devblogs.microsoft.com/typescript/walkthrough-interfaces/#describing-simple-types

// using `?: boolean`
<StyledButton /> // No error, noMargin is not required
// using `:boolean | undefined`
<StyledButton /> // Error, expected property `noMargin`...

相关问题