React TypeScript 样式报错

代码如下:

报错内容:

Type '{ flexDirection: string; }' is not assignable to type 'Properties<string | number, string & {}>'.

Types of property 'flexDirection' are incompatible.

Type 'string' is not assignable to type 'FlexDirection | undefined'.ts(2322)

index.d.ts(1942, 9): The expected type comes from property 'style' which is declared here on type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'

解决方案 1:

typescript 复制代码
const myStyle = {
  flexDirection: "column",
} as React.CSSProperties;

解决方案 2:

typescript 复制代码
class MyView extends Component {
  render() {
    return <div style={myStyle as React.CSSProperties}>my view</div>;
  }
}
相关推荐
奔跑的web.9 小时前
TypeScript Enum 类型入门:从基础到实战
前端·javascript·typescript
盐真卿9 小时前
python2
java·前端·javascript
梦梦代码精10 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
seabirdssss10 小时前
《bootstrap is not defined 导致“获取配置详情失败”?一次前端踩坑实录》
前端·bootstrap·html
kgduu11 小时前
js之表单
开发语言·前端·javascript
谢尔登12 小时前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码13 小时前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
我是伪码农13 小时前
轮播图案例
css·html·css3
欣然~14 小时前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python
一个小废渣14 小时前
Flutter Web端网络请求跨域错误解决方法
前端·flutter