我有一些快速模块增强。我的意图是能够做到:
request.message('Hello').status(400).json({});
status
和 json
函数是 Express Response
对象的一部分。这些函数返回调用它们的Response
对象以允许链接(又名流畅的API)。
我希望添加自己的 message
函数来完成同样的事情。我的增强看起来像这样:
import { SomeType } from '...';
declare global {
namespace Express {
interface Response {
// This works.
getSomeType(): SomeType;
// This does not. Typescript thinks the object returned here has ONLY the getSomeType/message functions on it
message(str: string): Response;
}
}
我尝试过
的变体message(str: string): Express.Response
还有
message(str: string): Response & Express.Response
这没有什么区别。和以前一样:Typescript 认为这里返回的对象只有 getSomeType/message 函数
最佳答案
也许如果你这样写就会起作用:
import { SomeType } from '...';
declare namespace Express {
export interface Response {
getSomeType(): SomeType;
message(str: string): Response;
}
}
Here是类似的问题
关于javascript - 使用 Fluent API 增强 Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55228408/