TypeScript 和 React Native : Are the type definitions for RN styles wrong?

标签 typescript react-native typescript-typings typescript-types

我认为在 React 中允许将数组分配给样式?

像这样:

import React from 'react';
import { Text } from "react-native";

// ...

render() {
  const textStyles = [styles.text];
  return <Text style={textStyles}>Some Text</Text>;
}

但是 TSLint 提示这条线:
[ts]
Type 'RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }>[]' is not assignable to type 'StyleProp<TextStyle>'.
  Type 'RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }>[]' is not assignable to type 'RecursiveArray<false | TextStyle | RegisteredStyle<TextStyle> | null | undefined>'.
Types of property 'pop' are incompatible.
  Type '() => RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }> | undefined' is not assignable to type '() => StyleProp<TextStyle>'.
    Type 'RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }> | undefined' is not assignable to type 'StyleProp<TextStyle>'.
      Type 'RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }> | undefined' is not assignable to type 'StyleProp<TextStyle>'.
      Type 'RegisteredStyle<{ color: string; fontWeight: string; fontSize?: undefined; padding: number; textAlign: "center"; } | { color: string; fontSize: number; fontWeight?: undefined; padding: number; textAlign: "center"; }>' is not assignable to type 'StyleProp<TextStyle>'.

这里发生了什么? React Native 的类型错了吗?或者我是否应该为 RN 样式对这些数组进行类型转换?

最佳答案

您可以测试一些方法来解决您的问题,希望能奏效:

1 :style={styles.yourStyle as any}也许你应该这样做!

2 :我看不到你如何实现你的风格,但你可以测试这样的东西?

import { StyleSheet, TextStyle, ViewStyle } from "react-native";

type Style = {
    container: ViewStyle;
    title: TextStyle;
    icon: ImageStyle;
};

export default StyleSheet.create<Style>({
    container: {
        flex: 1
    },
    title: {
        color: red
    },
    icon: {
        width: 10,
        height: 10
    }
});

关于TypeScript 和 React Native : Are the type definitions for RN styles wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639013/

相关文章:

angular - 在 typescript 中找不到名称 'exports' 错误

javascript - 密码确认 Angular Material

Typescript Private or protected member 'something' 无法在类型参数上访问

TypeScript:使用 0 个或多个参数调用重载的 asyncPipe

node.js - Typescript编译错误: Property 'bodyParser' does not exist on type 'typeof e'

javascript - Angular 2在同一页面中一个接一个地引导两个应用程序

css - React Native splinter 的边框

react-native - 清除或调用操作来重置状态?

react-native - navigation.setOptions 不会改变

javascript - Typescript - 如何将部分类型分配给另一个对象的相同类型的属性?