简体   繁体   English

Typescript 有条件的 Object 键:存在 Prop A 存在 Prob B

[英]Typescript Conditional Object Keys: Prop A exists Prob B must exist

Background背景

I want to conditionally type keys in an object based on the existence of another key in that object.我想根据 object 中是否存在另一个键,有条件地在 object 中键入键。

Problem问题

I have an object that has some required keys and some optional.我有一个 object 有一些必需的键和一些可选的。 I do not know how to make it so one of the optional keys is contingent upon the other optional key being present.我不知道如何制作,因此其中一个可选键取决于另一个可选键是否存在。 For instance,例如,

interface User { 
  username: string;
  id: string;
  member?: boolean; 
  memberStatus?: string;
}

Now, let's say I have a list of users that I am mapping through and I want to show a button if the user is a member and make that button a specific color based on memberStatus .现在,假设我有一个正在映射的用户列表,如果用户是member ,我想显示一个按钮,并根据memberStatus将该按钮设为特定颜色。

return users.map(user => (
  if(user.member) { 
    //memberStatus here will cause an error because it maybe a string or undefined
    <button className={memberStatus === 'active' ? 'green' : 'yellow'}>Member Button</button>
  }
  return (
    <p>{user.username} is a user</p>
  )
))

What I Tried我试过的

I looked up extending interfaces and conditional typing.我查找了扩展接口和条件类型。 However, extending a Member interface from the User interface but that still leaves me with optional keys.但是,从用户界面扩展了成员界面,但这仍然给我留下了可选的键。

Question问题

Is it possible to create an interface like so是否可以创建这样的界面

interface User { 
 username: string;
 id: string;
 member?: string;
 memberStatus: string; 
}

type UserOrMember = User.member ? User : Omit<User, 'memberStatus'>

apiUsers: UserOrMember = [...users]

Again, the goal is that memberStatus is only required if member is not undefined .同样,目标是memberStatus仅在member不是undefined时才需要。

Is there anything I am missing with extending interfaces or generics that could be helpful?扩展接口或 generics 是否有任何可能有用的东西?

You can try Tagged union types :您可以尝试标记联合类型

// declare types
interface BaseUser {
  username: string;
  id: string;
  member?: boolean;
}

interface User extends BaseUser {
  member?: false;
}

interface Member extends BaseUser {
  member: true;
  memberStatus: string;
}

type UserOrMember = User | Member;

// usage in component
declare const user: UserOrMember
console.log(user.member ? user.memberStatus : user.username)

Playground Link 游乐场链接

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

相关问题 TypeScript 强制转换为 Partial<T> 但密钥必须存在 - TypeScript cast as Partial<T> but keys must exist Typescript:防止 React 道具 object 的额外键? - Typescript: prevent extra keys for a React prop object? 在 Typescript 中删除对象键并返回条件类型 - Delete object keys and return conditional type in Typescript 有时根据参数存在于打字稿对象上的键 - Keys that sometimes exist on a typescript object depending on parameters Typescript 错误? Function 接受 Object 定义为接受类型 A 和 B 的联合,其中 B 扩展 A - Typescript bug? Function accepts Object with missing prop when defined to accept union of type A and B where B extends A 在检查条件属性是否存在时,Typescript 将 object 推断为从不 - Typescript infers object as never when checking if a conditional property exists 打字稿:对象的类型安全必须具有数组值中的所有键 - Typescript: typesafety for object must have all keys in array values 将对象属性约束为具有在 TypeScript 中的键必须相同的属性的对象 - Constrain objec properties to be object with properties whose keys must be the same in TypeScript 如何告诉 Typescript 两个对象键必须相同? - How can I tell Typescript that two object keys must be the same? 属性&#39;prop&#39;在类型&#39;A |中不存在。 B&#39; - Property 'prop' does not exist on type 'A | B'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM