英文:
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<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?
答案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'
};
英文:
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 => {
return {
user: '',
consent: false
}
};
const userPayload: UserDoc = {
...createSharedPayload(),
programs:[],
data: []
};
const adminPayload: AdminDoc = {
...createSharedPayload(),
src: 'some string'
}
答案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<BaseDoc> => {
return {
user: '',
consent: false
}
}
const userPayload: UserDoc = {
...createSharedPayload(),
programs: '',
data: ''
}
const adminPayload: AdminDoc = {
...createSharedPayload(),
src: ''
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论