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>;
  }
}
相关推荐
空中湖6 分钟前
用 React 写数据大屏:为什么我不建议用图表库
前端·react.js·前端框架
狮子座的男孩2 小时前
html+css基础:25、3D变换(3D空间与景深/透视点的位置/位移/旋转/缩放/多重变换/背部可见性)
css·经验分享·html·旋转·透视点·缩放/位移·3d变换/空间与景深
欣欣之王来了2 小时前
原始HTML插值:v-html指令的使用与安全风险
前端·学习·架构·项目·vue3教程
IT_陈寒2 小时前
Redis的LRU淘汰策略坑了我一整天
前端·人工智能·后端
计算机魔术师2 小时前
砍半价格、参数再涨,国产旗舰追到 Opus 鼻尖了
前端
特立独行的猫A2 小时前
10 MB 的桌面 AI 客户端:用仓颉 + 系统 WebView 替代 Electron
前端·harmonyos
修炼的dance3 小时前
白底图存JPG,sharp开mozjpeg背景杂点少近三成
前端
anOnion3 小时前
构建无障碍组件之Grid Pattern
前端·html·交互设计
离歌漠3 小时前
《我用 Tauri 做了一个本地开发工具箱》
rust·typescript·vue
乔旭伟3 小时前
Vben3 中后台表格封装实战:useVbenVxeGrid 的全局收敛与七个坑
前端·anti-design-vue·1024程序员节