简体   繁体   English

如何使用预定义的 object 键入 typescript 接口属性?

[英]How can I type a typescript interface property using a predefined object?

  • I have a typescript interface called Customer.我有一个名为 Customer 的 typescript 接口。
  • I want one property of that interface, orderAuthorizationStatus, to be only one of three strings: 'unauthorized', 'waitingForAuthorization' or 'authorized'我希望该接口的一个属性 orderAuthorizationStatus 只是三个字符串之一:'unauthorized'、'waitingForAuthorization' 或 'authorized'
  • At the same time I want to store these three strings somewhere, so I can use them for comparisons, translations etc., and not change them 10 different times should I decide to change the wording.同时我想将这三个字符串存储在某个地方,这样我就可以将它们用于比较、翻译等,而不是在我决定更改措辞时更改它们 10 次不同的时间。
  • I want to use the stored strings, for typing my interface property, I currently have this solution:我想使用存储的字符串来输入我的界面属性,我目前有这个解决方案:
export const OrderAuthorizationStates = {
  unauthorized: "unauthorized",
  waitingForAuthorization: "waitingForAuthorization",
  authorized: "authorized"
}

export interface Customer {
  id: number;
  name: string;
  orderAuthorizationStatus: "unauthorized" | "waitingForAuthorization" | "authorized"
}

This works and gives me the errors i want:这有效并给了我我想要的错误:

let customer: Customer;

if (customer.orderAuthorizationStatus === 'asdf') {
 somecode
}

Error: "This condition will always return 'false' since the types '"unauthorized" | "waitingForAuthorization" | "authorized"' and '"asdf"' have no overlap"错误:“此条件将始终返回‘false’,因为类型‘‘unauthorized’ |‘waitingForAuthorization’ |‘authorized’’和‘‘asdf’’没有重叠”
I can also use it like this:我也可以这样使用它:

let customer: Customer;

if (customer.orderAuthorizationStatus === OrderAuthorizationStates.authorized) {
 somecode
}

What I cannot solve is, how to use the objectproperties of my OrderAuthorizationStates object to type the interfaceproperty of my customer interface:我无法解决的是,如何使用我的 OrderAuthorizationStates object 的对象属性来键入我的客户界面的接口属性:

export interface Customer {
  id: number;
  name: string;
  orderAuthorizationStatus: OrderAuthorizationStates.unauthorized | OrderAuthorizationStates.waitingforAuthorization | OrderAuthorizationStates.authorized
}

Error: Cannot find namespace 'OrderAuthorizationStates'错误:找不到命名空间“OrderAuthorizationStates”

Help would be much appreciated!帮助将不胜感激!

String enums do almost exactly what you want: 字符串枚举几乎完全符合您的要求:

enum OrderAuthorizationStates {
    Unauthorized = "Unauthorized",
    WaitingForAuthorization = "WaitingForAuthorization",
    Authorized = "Authorized"
}

interface Customer {
    id: number;
    name: string;
    orderAuthorizationStatus: OrderAuthorizationStates;
}

let customer: Customer = { id: 1, name: "RichN", orderAuthorizationStatus: OrderAuthorizationStates.Unauthorized};
if (customer.orderAuthorizationStatus === "Unauthorized") {
    console.log("Customer is unauthorized");
}

// Authorize it!
customer.orderAuthorizationStatus = OrderAuthorizationStates.Authorized;

if (customer.orderAuthorizationStatus === OrderAuthorizationStates.Authorized) {
    console.log("Customer is now authorized");
}

// Output:
// Customer is unauthorized
// Customer is now authorized

In addition, if you do if (customer.orderAuthorizationStatus === 'asdf') you get the error 'This condition will always return 'false' since the types 'OrderAuthorizationStates' and '"asdf"' have no overlap.', which is what you want.此外,如果你这样做if (customer.orderAuthorizationStatus === 'asdf')你会得到错误'This condition will always return 'false' since the types 'OrderAuthorizationStates' and '"asdf"' have no overlap.',其中是你想要的。

Found a way without using enum.找到了一种不使用枚举的方法。

Create a type that is inferred from the object:创建一个从 object 推断出的类型:

export const OrderAuthorizationStatuses = {
    unauthorized: 'unauthorized',
    waitingForAuthorization: 'waitingForAuthorization',
    authorized: 'authorized'
}

type OrderAuthorizationStatus = typeof OrderAuthorizationStatuses[keyof typeof OrderAuthorizationStatuses]

Now you can use the object keys as variables throughout your application, without any type missunderstandings with enum.member and you can use all the object methods on it, plus you can use it to type your interface properties, or anything really:现在,您可以在整个应用程序中使用 object 键作为变量,而不会对 enum.member 有任何类型误解,您可以在其上使用所有 object 方法,此外,您还可以使用它来键入接口属性或任何其他内容:

export interface Customer {
  id: number;
  name: string;
  orderAuthorizationStatus: OrderAuthorizationStatus
}

source 来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在打字稿上制作多类型属性界面 - How can i make multiple type property interface on typescript 如何正确键入 object 和 typescript 与属性 object - How can i correctly type an object with typescript with condition on property object 如何在typescript对象接口中键入字符串作为Object属性键? - How to type a string as an Object property-key in a typescript Object interface? 在 TypeScript 中,我如何在 go 中向接口中的 object 类型添加属性? - In TypeScript, How do I go about adding a property to an object type in an Interface? 当属性名称相同时,如何使用 typescript 将 map 和 object 转换为另一个接口类型 - How to map an object to another interface type when the property names are the same using typescript 如何在 typescript 中获取 object 的接口? - How can I get the interface of an object in typescript? 如何访问用Typescript中的接口声明的属性的属性? - How can I access a property of a property declared with an interface in Typescript? 在 Typescript 中,如何定义一个 class 接口,其中包含一个属性成员,该属性成员是可区分的联合类型? - In Typescript, how can I define a class interface containing a property member that is a discriminated union type? 如何使用对接口中属性的引用作为 TypeScript 中的类型? - How do I use a reference to a property in an interface as a type in TypeScript? 如何通过类型而不是接口使用 Typescript 到 model 和 object? - How can I use Typescript to model an object via type instead of an interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM