【问题标题】:How can I declare an object property in TypeScript?如何在 TypeScript 中声明对象属性?
【发布时间】:2021-09-19 09:17:37
【问题描述】:

这段代码 sn-p 给了我这个错误:

键入'JustifyContent | undefined' 不可分配给类型 '"space-around" | “之间的空间” | “空间均匀” | “中心” | “弹性端” | “弹性开始” |不明确的'。 类型 'string & {}' 不可分配给类型 '"space-around" | “之间的空间” | “空间均匀” | “中心” | “弹性端” | “弹性开始” |未定义'。

我需要帮助来解决这个问题。

欢迎对编写代码提出建设性的批评。

【问题讨论】:

  • 欢迎堆栈溢出!如果您将代码作为文本而不是图像发布在问题中,这会有所帮助。然后我们可以轻松地复制/粘贴/更新它。

标签: typescript react-native react-native-stylesheet


【解决方案1】:

您正在尝试在反应原生样式表中使用 CSS 类型(在网络上)。那是行不通的。 React 原生样式表深受基于 Web 的 CSS 的启发,但它们是另一回事。

查看类型,您需要来自react-native 模块的FlexStyle['justifyContent']

一个非常简单的例子:

import { StyleSheet, FlexStyle } from 'react-native'

const justify: FlexStyle['justifyContent'] = 'center'

const styles = StyleSheet.create({
  foo: {
    justifyContent: justify // works fine
  }
})

我会给你一个更完整的例子,但我不会重新输入你图片中的所有代码。尽管如此,这应该为您提供正确的类型。

Playground

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多