ES6箭头函数与TypeScript

3

我对TypeScript非常陌生,不知道如何使其工作。

查了一些资料,像是解构(destructuring),但仍然不能正确运行。

import React from "react";
import { StyleSheet, Text, View } from "react-native";

const styles = {
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
};

const App = (style: any): {} => {
  const { container } = styles;
  return (
    <View style={container}>
      <Text>Open up App.tsx to start working on your app!</Text>
    </View>
  );
};

export default App;

下面是错误信息:
(JSX attribute) style?: StyleProp<ViewStyle>
Type '{ flex: number; backgroundColor: string; alignItems: string; justifyContent: string; }' is not assignable to type 'StyleProp<ViewStyle>'.
  Type '{ flex: number; backgroundColor: string; alignItems: string; justifyContent: string; }' is not assignable to type 'ViewStyle'.
    Types of property 'alignItems' are incompatible.
      Type 'string' is not assignable to type 'FlexAlignType'.ts(2322)
index.d.ts(2206, 5): The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps> & Readonly<{ children?: ReactNode; }>'

刚刚添加了错误信息。 - Dhaval Jardosh
3个回答

2

明确指定“容器”的类型。你可以使用来自react-native包的通用ViewStyle类型:

import React from "react";
import { StyleSheet, Text, View, ViewStyle } from "react-native";

const styles = {
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  } as ViewStyle
};

const App = () => {
  const { container } = styles;
  return (
    <View style={container}>
      <Text>Open up App.tsx to start working on your app!</Text>
    </View>
  );
};

export default App;

需要更加熟悉ViewStyles和其他RN开箱即用的东西,但这个方法可行。谢谢。 - Dhaval Jardosh
实际上这是 TypeScript 的类型推断问题。它推断 "alignItems" 的类型为 "string",但 React 需要字符串字面量类型。您可以在此处查看更多详细信息:https://github.com/Microsoft/TypeScript/issues/9336 - Alex
我对这个答案有几个疑虑。1. App 接受了一个名为 style 的参数,但实际上它根本没有被使用(使用的是一个名为 styles 的局部变量)。2. App 的返回类型被定义为空对象,而实际上它应该是 ReactNode。我怀疑当前的实现可能无法与 TypeScript 兼容。 - Ben Carp
@BenCarp 你说得对,我只是展示了如何解决作者遇到的问题,但并没有解决其中一些问题。不过在 TypeScript 中仍然可以工作。我进行了编辑以解决这些问题。 - Alex

1
const styles = {
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
};

const App: React.SFC = () => {
   return 
      <View style={styles.container}>
         <Text>Open up App.tsx to start working on your app!</Text>
      </View>
   )
}
  • React默认不支持Typescript,因此请确保在您的package.json中安装了@typescript/react

@types/react@typescript/react,它们是同一件事吗? - Dhaval Jardosh
@types/react 为 React 的 API 添加了所有 TypeScript 类型定义。例如 - React.FC(函数式组件), - Ben Carp
1
我认为这应该也能工作,我需要更多地了解。非常感谢你的帮助,Ben。 - Dhaval Jardosh

0

你应该添加你的样式变量的类型

import React from "react";
import { StyleSheet, Text, View } from "react-native";

type Style = {
  [key: string]: Object;
};

type ViewStyles = {
  container: Style;
};

const styles: ViewStyles = {
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
};

const App: React.SFC = () => {
  const { container }: Style = styles;
  return (
    <View style={container}>
      <Text>Open up App.tsx to start working on your app!</Text>
    </View>
  );
};

export default App;

1
这难道不会使一切都变成无类型吗? - Alex
那怎么样?一切都是无类型的吗? - Aziz.G
容器将是类型为 Object 的对象,基本上是具有任何属性的对象。 - Alex
当然你可以添加另一种类型到类型中,但是里面有CSS属性,你要全部写出来吗? - Aziz.G
3
请指定合适的类型,例如 ViewStyle - Alex
让我们在聊天室里继续这个讨论 - Aziz.G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接