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>;
  }
}
相关推荐
大今野1 分钟前
2026 专转本计算机|移动互联网关键技术:SOA、Web2.0 与 HTML5 考点全解(附例题)
前端·html·html5·专转本
JudithHuang21 分钟前
Vue3 与后端联调:代理、Mock 与接口约定
前端·vue
Swot28 分钟前
工具转换 vs LLM 转换:以 Markdown 转 AsciiDoc 为例
前端
uuware1 小时前
Lupine.js 的 Native 5 端实现:一套代码覆盖 iOS、Android、macOS、Windows 和 Web
前端·typescript
lisw051 小时前
前端:概念、历史、内容、挑战与展望!
前端
stormsha1 小时前
ResizeObserver loop completed with undelivered notifications:它只是个警告,但也是你代码的供词
javascript·css
ZHOUPUYU2 小时前
PHP 现代工程实践:从语言特性到高性能 Web 应用构建
android·前端·php
zhanlang02174 小时前
1.2 CSS 盒模型深度剖析:标准与怪异模式的博弈
前端·css
徐同保4 小时前
模板里的图标组件找不到定义?聊聊 @tabler/icons-vue 与自动导入
前端·javascript·vue.js
喵本喵叁肆4 小时前
ReAct、Plan-Execute、Reflection:三种 Agent 模式实测对比
前端·react.js·前端框架