【发布时间】:2020-10-07 12:10:31
【问题描述】:
我从 AWS Amplify 教程中复制了以下内容:
const styles = {
container: { flexDirection: 'column' },
...
};
<div style={styles.container}>
但我得到了:
Type '{ flexDirection: string; }' is not assignable to type 'CSSProperties'.
Types of property 'flexDirection' are incompatible.
Type 'string' is not assignable to type '"column" | "inherit" | "-moz-initial" | "initial" | "revert" | "unset" | "column-reverse" | "row" | "row-reverse" | undefined'.
我可以通过将样式内联来轻松解决这个问题,但我很好奇 TypeScript 为何认为这是一个错误。这是一个非常基本的示例,我很惊讶 TS 在它上面失败了,所以这让我对使用 TS 保持警惕。
【问题讨论】:
-
如果它是有角度的,那么这应该是语法
<div [style]="styles.container"> -
这是 React,添加到问题
-
房产为
flex-direction: column请查收 -
@CharlesBamford 否,因为该问题中的作者明确将文字转换为
string。在我的演员阵容中,TS 将styles.container.flexDirection的类型推断为string,而不是文字'column'。我不知道为什么它不将其视为文字
标签: javascript reactjs typescript