如何禁用特定接口的显式键入
How to disable explicit typing for a certain interface
我的项目中有一个错误渲染器函数。
它创建了两个函数
res.errorJson // Will only send a JSON body to the client
res.error // Will send an HTML error if the client is a browser. Otherwise we'll send json
在你问之前,是的,我确保在 express.d.ts
中为这两个函数添加类型
res.error
有两个参数,第一个是状态码,第二个是我们要在响应中发送的参数。
这是我的错误选项界面
// res.error(statuscode: number, options: errorOptions)
declare interface errorOptions {
title?: string;
description?: string | Array<string>;
customHTML?: string;
jsonOnly?: boolean
}
假设调用 res.error 有一个不在界面内的附加选项,打字稿会冲我大喊大叫
我想禁用此 ONE 界面的显式输入。
基本上就是这样我在界面中有预类型,如果我想向选项添加其他内容,我可以这样做而无需打字稿对我大喊大叫。我希望这是有道理的,我希望我不傻
这是我的错误渲染器的所有代码
res.errorJson = (statusCode: RangeOf2<100,599>, options?: errorOptions) => {
options.jsonOnly = true;
return res.error(statusCode, options);
};
res.error = (statusCode: RangeOf2<100,599>, options?: errorOptions) => {
if (typeof(statusCode) !== "number") {
throw new Error("Status code is not a number");
}
options = options || {};
if (Object.prototype.hasOwnProperty.call(errors, statusCode)) {
options.title = errors[statusCode];
} else {
options.title = options.title || `${statusCode} Not defined`;
}
if (req.useragent.isBot !== false || options.jsonOnly === true) {
let data = {
success: false
};
for (const v in data) {
console.log(v);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore ignore this until espi gets back to me
data.success = false;
res.send(JSON.stringify(data, null, 2) + "\n");
return data;
}
ejs.renderFile(path.join(__dirname, "errorTemplate.ejs"), {data: options}).then(render => {
try {
res.status(statusCode).send(render);
} catch (error) {
console.error("You are sending an error after headers were sent to the client.");
console.log(error);
}
});
return options;
};
next();
为此,您应该能够声明您的接口以接受任意参数:
declare interface errorOptions {
title?: string;
description?: string | Array<string>;
customHTML?: string;
jsonOnly?: boolean;
[x: string]: any;
}
“如果目标类型接受额外的属性,添加一个索引器”
我的项目中有一个错误渲染器函数。 它创建了两个函数
res.errorJson // Will only send a JSON body to the client
res.error // Will send an HTML error if the client is a browser. Otherwise we'll send json
在你问之前,是的,我确保在 express.d.ts
res.error
有两个参数,第一个是状态码,第二个是我们要在响应中发送的参数。
这是我的错误选项界面
// res.error(statuscode: number, options: errorOptions)
declare interface errorOptions {
title?: string;
description?: string | Array<string>;
customHTML?: string;
jsonOnly?: boolean
}
假设调用 res.error 有一个不在界面内的附加选项,打字稿会冲我大喊大叫
我想禁用此 ONE 界面的显式输入。
基本上就是这样我在界面中有预类型,如果我想向选项添加其他内容,我可以这样做而无需打字稿对我大喊大叫。我希望这是有道理的,我希望我不傻
这是我的错误渲染器的所有代码
res.errorJson = (statusCode: RangeOf2<100,599>, options?: errorOptions) => {
options.jsonOnly = true;
return res.error(statusCode, options);
};
res.error = (statusCode: RangeOf2<100,599>, options?: errorOptions) => {
if (typeof(statusCode) !== "number") {
throw new Error("Status code is not a number");
}
options = options || {};
if (Object.prototype.hasOwnProperty.call(errors, statusCode)) {
options.title = errors[statusCode];
} else {
options.title = options.title || `${statusCode} Not defined`;
}
if (req.useragent.isBot !== false || options.jsonOnly === true) {
let data = {
success: false
};
for (const v in data) {
console.log(v);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore ignore this until espi gets back to me
data.success = false;
res.send(JSON.stringify(data, null, 2) + "\n");
return data;
}
ejs.renderFile(path.join(__dirname, "errorTemplate.ejs"), {data: options}).then(render => {
try {
res.status(statusCode).send(render);
} catch (error) {
console.error("You are sending an error after headers were sent to the client.");
console.log(error);
}
});
return options;
};
next();
为此,您应该能够声明您的接口以接受任意参数:
declare interface errorOptions {
title?: string;
description?: string | Array<string>;
customHTML?: string;
jsonOnly?: boolean;
[x: string]: any;
}
“如果目标类型接受额外的属性,添加一个索引器”