【发布时间】:2021-05-03 14:36:36
【问题描述】:
我在尝试定义继承模型以便从我的 API 做出通用响应时遇到问题。
关键是我所有的响应当前都有一个状态和一个状态消息加上响应的内容在同一级别进入对象。
所以 resp1 结构很容易定义为 ResponseBaseContentOf,但是如何使用 ResponseBaseInheritsOf 之类的东西来定义 resp2 响应样式?你能看出区别吗?
export type ResponseStatus = 'OK' | 'NOK'
export const ResponseStatus = {
Ok: 'OK' as ResponseStatus,
Nok: 'NOK' as ResponseStatus
}
export interface ResponseBase {
status: ResponseStatus,
message: string
}
export interface ResponseBaseContentOf<T> extends ResponseBase {
content: T
}
export interface MyClass {
property01: string
property02: number
}
const resp1: ResponseBaseContentOf<MyClass> = {
status: 'OK',
message: 'response ok',
content: {
property01: '01',
property02: 2,
}
}
ResponseBaseInheritsOf<MyClass>
const resp2 = {
status: 'OK',
message: 'response ok',
property01: '01',
property02: 2
}
【问题讨论】:
-
请提供 NOK 回复
标签: typescript api inheritance response