升级Next.js 13.3.1后styled-components报AnyStyledComponent类型错误求助
解决Next.js 13.3.1 + styled-components的TS2769类型错误
下面是几个可行的解决方案,按需选择:
显式类型断言:直接给Switch组件做类型转换,让它兼容
AnyStyledComponentimport styled from 'styled-components'; import Switch from 'react-switch'; import { AnyStyledComponent } from 'styled-components'; const StyledSwitch = styled(Switch as unknown as AnyStyledComponent)` // 你的样式代码 `;或者用更简单的
as any临时绕过(适合快速验证,但不推荐长期使用):const StyledSwitch = styled(Switch as any)` // 你的样式代码 `;同步类型定义版本:检查
@types/styled-components和styled-components的版本是否匹配,版本不一致容易引发类型冲突。执行命令更新类型包:npm install @types/styled-components@latest -D # yarn用户执行: yarn add @types/styled-components@latest -D封装类型兼容的包装组件:如果不想每次都写断言,可以先封装一个类型处理后的Switch组件:
import Switch from 'react-switch'; import { AnyStyledComponent } from 'styled-components'; const TypedSwitch = Switch as unknown as AnyStyledComponent; const StyledSwitch = styled(TypedSwitch)` // 你的样式代码 `;临时放宽类型检查:如果以上方法都没效果,可以在
tsconfig.json中开启skipLibCheck跳过库类型检查(仅应急使用,长期可能掩盖其他类型问题):{ "compilerOptions": { "skipLibCheck": true } }
内容的提问来源于stack exchange,提问作者ivoba
相关产品推荐
相关产品推荐

