Typescript 接口没有重载/合并后续接口字段
Typescript interface are not overloading / merging subsequent interface fields
我知道打字稿中的接口允许我们合并不同的类型。当我尝试这样做时,我在转译脚本时遇到错误。
这是我的错误界面
export interface StoreConfig extends Document, TimeStamps {
type: 'webhook'
metadata: {
endpoint: string
method: HttpMethod
secret: string
event: string | string[]
}
}
export interface StoreConfig extends Document, TimeStamps {
type: 'buylink'
metadata: {
prodId: string
key: string
expire: Date
}
}
export interface StoreConfig extends Document, TimeStamps {
type: 'paymentmethod'
metadata: {
apiKey: string
mode: string
whsecret: string
}
}
我在转译 ts 脚本时遇到这个错误
Subsequent property declarations must have the same type. Property 'type' must be of type '"webhook"', but here has type '"buylink"'.
PS:我看到很多库(例如:nodemailer、inquirer)都在根据一些标志或条件加载类型。
/**
* Simplified example
*/
export interface StoreConfig extends Document {
type: 'webhook'
}
export interface StoreConfig extends Document {
type: 'buylink'
}
export interface StoreConfig extends Document {
type: 'paymentmethod'
}
/**
* This is how it works
* 1)
*/
export interface A {
type: 'buylink'
}
export interface A {
payload: number
}
type O = keyof A // type | payload
/**
* Because you can't do smth like that
*/
type CustomType = 'buylink' & 'webhook' // never
/**
* Above type is never because it is irrepresentable
* Please treat merging as & operator on high level
*/
你需要做的是做一个联合类型。就像@ritaj 在他的评论中写的那样:
export interface StoreConfig1 extends Document {
type: 'webhook'
}
export interface StoreConfig2 extends Document {
type: 'buylink'
}
export interface StoreConfig3 extends Document {
type: 'paymentmethod'
}
type StoreConfig = StoreConfig1 | StoreConfig2 | StoreConfig3
我知道打字稿中的接口允许我们合并不同的类型。当我尝试这样做时,我在转译脚本时遇到错误。
这是我的错误界面
export interface StoreConfig extends Document, TimeStamps {
type: 'webhook'
metadata: {
endpoint: string
method: HttpMethod
secret: string
event: string | string[]
}
}
export interface StoreConfig extends Document, TimeStamps {
type: 'buylink'
metadata: {
prodId: string
key: string
expire: Date
}
}
export interface StoreConfig extends Document, TimeStamps {
type: 'paymentmethod'
metadata: {
apiKey: string
mode: string
whsecret: string
}
}
我在转译 ts 脚本时遇到这个错误
Subsequent property declarations must have the same type. Property 'type' must be of type '"webhook"', but here has type '"buylink"'.
PS:我看到很多库(例如:nodemailer、inquirer)都在根据一些标志或条件加载类型。
/**
* Simplified example
*/
export interface StoreConfig extends Document {
type: 'webhook'
}
export interface StoreConfig extends Document {
type: 'buylink'
}
export interface StoreConfig extends Document {
type: 'paymentmethod'
}
/**
* This is how it works
* 1)
*/
export interface A {
type: 'buylink'
}
export interface A {
payload: number
}
type O = keyof A // type | payload
/**
* Because you can't do smth like that
*/
type CustomType = 'buylink' & 'webhook' // never
/**
* Above type is never because it is irrepresentable
* Please treat merging as & operator on high level
*/
你需要做的是做一个联合类型。就像@ritaj 在他的评论中写的那样:
export interface StoreConfig1 extends Document {
type: 'webhook'
}
export interface StoreConfig2 extends Document {
type: 'buylink'
}
export interface StoreConfig3 extends Document {
type: 'paymentmethod'
}
type StoreConfig = StoreConfig1 | StoreConfig2 | StoreConfig3