React TypeScript 样式报错

代码如下:
React TypeScript 样式报错_第1张图片

报错内容:
React TypeScript 样式报错_第2张图片

Type ‘{ flexDirection: string; }’ is not assignable to type ‘Properties’.
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

解决方案 1:

const myStyle = {
  flexDirection: "column",
} as React.CSSProperties;

解决方案 2:

class MyView extends Component {
  render() {
    return <div style={myStyle as React.CSSProperties}>my view</div>;
  }
}

你可能感兴趣的:(React,typescript,前端,react,css)