如何在 TypeScript 中的对象常量内为属性分配类型?

塞伯特

学习 TypeScript,我想知道当 TypeScript 不能正确推断属性类型时,是否有一种简单的内联方法来表示对象常量内的类型?

错误信息:

Type '{ align: string; fontWeight: string; dropShadow: boolean; dropShadowAlpha: number; wordWrap: boolean; fill: string[]; }' is not assignable to type 'TextStyle | Partial<ITextStyle> | undefined'.
  Type '{ align: string; fontWeight: string; dropShadow: boolean; dropShadowAlpha: number; wordWrap: boolean; fill: string[]; }' is not assignable to type 'Partial<ITextStyle>'.
    Types of property 'align' are incompatible.
      Type 'string' is not assignable to type 'TextStyleAlign | undefined'.ts(2322)

在我用 textStyleAlign 和 textStyleFontWeight 替换原始 align 和 fontWeight 值之前收到此错误消息。

我是如何创建这个例子的:

yarn create react-app app-name --template typescript
cd app-name
yarn add pixi.js
yarn add @inlet/react-pixi

应用程序.tsx

import { Stage, Text} from '@inlet/react-pixi';
import type { TextStyleAlign, TextStyleFontWeight } from '@pixi/text';

const textStyleAlign : TextStyleAlign = 'center';
const textStyleFontWeight : TextStyleFontWeight = '900';

const textStyle = {
  align: textStyleAlign,
  // align: 'center',   // <- why can't I put this instead?
  fontWeight: textStyleFontWeight,
  // fontWeight: '900', // <- same here
  dropShadow: true,
  dropShadowAlpha: 0.6,
  wordWrap: true,
  fill: ["white", "#cccccc"]
}

function App() {
  return (
    <Stage>
      <Text
        text={`Hi World`}
        style={textStyle}
      />
    </Stage>
  );
}

export default App;
让182

嘿,通过查看您需要像这样传递 TextStyle 实例的文档:

const textStyle = new TextStyle({
  align: "center",
  fontWeight: "900",
  dropShadow: true,
  dropShadowAlpha: 0.6,
  wordWrap: true,
  fill: ["white", "#cccccc"]
});

如果您不想创建该实例,您可以这样做:

import { ITextStyle, TextStyle } from "pixi.js";

// The style prop also takes a Partial of ITextStyle so by typing the const like that you would achieve the typing correctly
const textStyle: Partial<ITextStyle> = {
  align: "center",
  fontWeight: "900",
  dropShadow: true,
  dropShadowAlpha: 0.6,
  wordWrap: true,
  fill: ["white", "#cccccc"]
};

如果您想让它像您发布的代码一样,那么您可以像这样使用 as 类型断言

const textStyle = {
  align: "center" as TextStyleAlign,
  fontWeight: "900" as TextStyleFontWeight,
  dropShadow: true,
  dropShadowAlpha: 0.6,
  wordWrap: true,
  fill: ["white", "#cccccc"]
};

如果你通过这 3 种方法中的任何一种,你就不会有类型问题:

<Text text="Hi World" style={textStyle} />

我注意到通过检查 Text 组件的 style 属性:

style?: TextStyle | Partial<ITextStyle> | undefined

所以基本上上面完成的所有打字都可以工作。

检查此沙箱以查看功能代码:https : //codesandbox.io/s/elastic-platform-zzvnb

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Typescript:如何在React中为历史对象添加类型检查?

如何在TypeScript中为对象动态分配属性?

如何在TypeScript中为类属性动态分配值

如何在Typescript中为箭头函数创建通用类型

如何在TypeScript中覆盖类型属性

使用映射类型时,如何在TypeScript中推断对象属性的值?

如何在TypeScript中为具有动态属性的对象定义类型

如何在Typescript中打印自定义对象的属性?

如何在Typescript中为相互关联的属性建模

如何基于TypeScript中的其他类型使对象属性为可选?

如何在TypeScript中防止文字类型

如何在TypeScript中为咖喱函数添加类型?

属性是可选的时,如何在Typescript中解构对象?

如何在Typescript中按属性过滤对象类型的并集?

如何在TypeScript中为动态“ this”值定义类型?

如何在Angular HTML属性中编写复杂的Typescript对象

对象或布尔值如何在Typescript中声明类型

如何在TypeScript中动态访问对象属性

如何在TypeScript中获取接口属性的类型?

Typescript如何从常量属性值推断类型

如何在Typescript中为对象数组声明数据类型?

如何在TypeScript中为函数包装添加类型?

如何在 TypeScript 中为函数类型添加键?

如何根据 switch case 为 TypeScript 中的对象分配类型

如何在 Typescript 中返回 NonNullable 类型?

如何在 Typescript 中为类成员分配属性

如何在 TypeScript 中定义类型的互斥属性?

如何在 TypeScript 中声明对象属性?

如何在TypeScript中声明通过for await创建的对象的类型