在一个React-Native应用程序中,我有以下样式:
为什么编译器会抱怨不兼容?看起来它正在推断
我知道我可以使用
const styles = StyleSheet.create({
text: {
textAlign: "center"
},
})
在<Text style={styles.text} />
中使用,但是tsc
编译器会报错如下:
Types of property 'textAlign' are incompatible.
Type 'string' is not assignable to type '"center" | "auto" | "left" | "right"'.
@types/react-native
中的 TextStyle
定义包括:export interface TextStyle extends TextStyleIOS, TextStyleAndroid,
ViewStyle {
// ...
textAlign?: "auto" | "left" | "right" | "center"
// ...
}
为什么编译器会抱怨不兼容?看起来它正在推断
textAlign
的类型为过于通用的string
,而不是检查实际值("center"
)。我知道我可以使用
as TextStyle
来避免这个问题,但我想知道为什么会发生这种情况,以及是否应该向编译器提交错误报告。
string
。否则,拥有字符串字面类型的目的是什么? - Ralph