如何创建一个符合接口要求的部分载荷?

huangapple go评论64阅读模式
英文:

How to create a partial payload that adheres to an interface?

问题

I have the following code where I have 2 interfaces that have shared data. Since the real-life object is so big I don't want to repeat myself to avoid errors and keep the code solid. However, I cannot figure out how to implement this in a way that would make it compatible. I keep getting an error like there are fields that are missing.

export interface UserDoc {
   user: string;
   consent: boolean;
   programs: string;
   data: string;
}
 
export interface AdminDoc extends Omit<UserDoc, 'programs' | 'data'> {
    user: string;
    consent: boolean;
    src: string;
}

// Shared fields
export const createSharedPayload = (): Partial<UserDoc | AdminDoc> => {
  return {
     user: '',
     consent: false,
  };
}

const userPayload: UserDoc = {
     ...createSharedPayload(),
   programs: [],
   data: []
}

const adminPayload: AdminDoc = {
     ...createSharedPayload(), 
     src: 'some string'
}

How to make this bundle work together?

英文:

I have the following code where I have 2 interfaces that have shared data. Since the real life object is so big I don't want to repeat myself to avoid errors and keep the code solid. However I cannot figure out how to implement this in a way that would make it compatible. I keep getting an error like there are fields that are missing.

export interface UserDoc {
   user: string;
   consent: boolean;
   programs: string;
   data: string;
}
 
export interface AdminDoc extends Omit&lt;UserDoc, &#39;programs&#39; | &#39;data&#39;&gt; {
user: string;
consent: boolean;
src: string;
}

// Shared fields
export const createSharedPayload = (): Partial&lt;UserDoc | AdminDoc&gt; =&gt; {
  return {
     user: &#39;&#39;,
     consent: false,
}

const userPayload: UserDoc = {
     ...createSharedPayload(),
   programs:[],
   data: []
}

const adminPayload: AdminDoc = {
     ...createSharedPayload(), 
     src: &#39;some string&#39;
}

How to make this bundle work together?

答案1

得分: 1

以下是代码的翻译部分:

标准的面向对象编程(OOP)方法是将共享字段抽象到一个通用的超级接口中:

interface Doc {
   user: string;
   consent: boolean;
}

export interface UserDoc extends Doc {
   programs: string[];
   data: string[];
}

export interface AdminDoc extends Doc {
  src: string;
}

export const createSharedPayload = (): Doc => {
  return {
     user: '',
     consent: false
  };
};

const userPayload: UserDoc = {
     ...createSharedPayload(),
   programs:[],
   data: []
};

const adminPayload: AdminDoc = {
     ...createSharedPayload(), 
     src: 'some string'
};

Playground链接

英文:

The standard OOP approach would be to abstract away the shared fields in a common super-interface:

interface Doc {
   user: string;
   consent: boolean;
}

export interface UserDoc extends Doc {
   programs: string[];
   data: string[];
}
 
export interface AdminDoc extends Doc {
  src: string;
}

export const createSharedPayload = (): Doc =&gt; {
  return {
     user: &#39;&#39;,
     consent: false
  }
};

const userPayload: UserDoc = {
     ...createSharedPayload(),
   programs:[],
   data: []
};

const adminPayload: AdminDoc = {
     ...createSharedPayload(), 
     src: &#39;some string&#39;
}

Playground link

答案2

得分: 1

你应该创建一个名为 BaseDoc 的基类来定义共有属性,而不是使用 Omit 删除一些属性。

export interface BaseDoc  {
  user: string;
  consent: boolean;
}

export interface AdminDoc extends BaseDoc {
  src: string;
}

export interface UserDoc extends BaseDoc {
  programs: string;
  data: string
}

export const createSharedPayload = (): Partial<BaseDoc> => {
  return {
    user: '',
    consent: false
  }
}

const userPayload: UserDoc = {
  ...createSharedPayload(),
  programs: '',
  data: ''
}

const adminPayload: AdminDoc = {
  ...createSharedPayload(),
  src: ''
}
英文:

Instead of using Omit to remove some properties, you should instead create a base class BaseDoc to define the common ones.

export interface BaseDoc  {
  user: string;
  consent: boolean;
}

export interface AdminDoc extends BaseDoc {
  src: string;
}

export interface UserDoc extends BaseDoc {
  programs: string;
  data: string
}

export const createSharedPayload = (): Partial&lt;BaseDoc&gt; =&gt; {
  return {
    user: &#39;&#39;,
    consent: false
  }
}

const userPayload: UserDoc = {
  ...createSharedPayload(),
  programs: &#39;&#39;,
  data: &#39;&#39;
}

const adminPayload: AdminDoc = {
  ...createSharedPayload(),
  src: &#39;&#39;
}

huangapple
  • 本文由 发表于 2023年5月11日 14:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定