【发布时间】:2019-03-14 07:27:39
【问题描述】:
我想扩展 Mocha 的 describe 与 forUsers 函数,这将创建多个描述。每个用户一个。
原始描述定义:
interface IContextDefinition {
(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
only(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
skip(description: string, callback: (this: ISuiteCallbackContext) => void): void;
timeout(ms: number): void;
}
我的扩展:
declare namespace Mocha {
interface IContextDefinition {
forUsers(description: string, users: userType[], callback: (this: ISuiteCallbackContext, user: Cypress.userType) => void): void
only: {
(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite
forUsers(description: string, users: userType[], callback: (this: ISuiteCallbackContext, user: userType) => void): void
}
skip: {
(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite
forUsers(description: string, users: userType[], callback: (this: ISuiteCallbackContext, user: userType) => void): void
}
}
}
但我收到此错误:
Subsequent property declarations must have the same type. Property 'only' must be of type '(description: string, callback: (this: ISuiteCallbackContext) => void) => ISuite', but here has type '{ (description: string, callback: (this: ISuiteCallbackContext) => void): ISuite; forUsers(description: string, users: userType[], callback: (this: ISuiteCallbackContext, user: userType) => void): void; }'.
那个only,只能是原始类型,即使新类型包含旧类型。
【问题讨论】:
-
你为什么要覆盖定义?如果您只是在命名空间中定义自己的定义,我认为会更好
-
文件是有作用域的,这意味着,除非导出,否则变量不会共享,并且由于我不希望其他人导入此函数,因此声明
describe.forUsers听起来是最好的地方。
标签: typescript interface extending