Node.js 为原型函数添加类型
我在节点模块中有一个JavaScriptNode.js 为原型函数添加类型,node.js,typescript,Node.js,Typescript,我在节点模块中有一个JavaScript函数,如下所示(简化): index.js: var BadRequestError = require('./error') Guard.prototype = { /** * * @param {String} property * @return {_middleware} */ check: function (property) { const _middleware = function _middl
函数
,如下所示(简化):
index.js
:
var BadRequestError = require('./error')
Guard.prototype = {
/**
*
* @param {String} property
* @return {_middleware}
*/
check: function (property) {
const _middleware = function _middleware (req, res, next) {
const ok = property === 'ping'
next(!ok ? new BadRequestError('doing_it_wrong', { message: `you're doing it wrong`}) : null)
}
return _middleware
}
}
module.exports = function (options) {
return new Guard(options)
}
module.exports = function BadRequestError (code, error) {
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = error.message
this.code = code
this.status = 400
this.inner = error
}
util.inherits(module.exports, Error)
var guard = require('../index')({
someProperty: 'test',
})
var req = {}
guard.check('ping')(req, res, function (err) {
assert.deepStrictEqual(null, err)
})
error.js
:
var BadRequestError = require('./error')
Guard.prototype = {
/**
*
* @param {String} property
* @return {_middleware}
*/
check: function (property) {
const _middleware = function _middleware (req, res, next) {
const ok = property === 'ping'
next(!ok ? new BadRequestError('doing_it_wrong', { message: `you're doing it wrong`}) : null)
}
return _middleware
}
}
module.exports = function (options) {
return new Guard(options)
}
module.exports = function BadRequestError (code, error) {
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = error.message
this.code = code
this.status = 400
this.inner = error
}
util.inherits(module.exports, Error)
var guard = require('../index')({
someProperty: 'test',
})
var req = {}
guard.check('ping')(req, res, function (err) {
assert.deepStrictEqual(null, err)
})
用法如下:
test.js
:
var BadRequestError = require('./error')
Guard.prototype = {
/**
*
* @param {String} property
* @return {_middleware}
*/
check: function (property) {
const _middleware = function _middleware (req, res, next) {
const ok = property === 'ping'
next(!ok ? new BadRequestError('doing_it_wrong', { message: `you're doing it wrong`}) : null)
}
return _middleware
}
}
module.exports = function (options) {
return new Guard(options)
}
module.exports = function BadRequestError (code, error) {
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = error.message
this.code = code
this.status = 400
this.inner = error
}
util.inherits(module.exports, Error)
var guard = require('../index')({
someProperty: 'test',
})
var req = {}
guard.check('ping')(req, res, function (err) {
assert.deepStrictEqual(null, err)
})
我尝试使用几种方法为其创建TypeScript打字,但似乎都不起作用:
index.d.ts
:
export declare interface IGuardOptions {
property: string
}
通过class
声明导出:
export declare class Guard {
constructor(options: IGuardOptions)
check(required: string | Array<string>): any
}
export declare interface Guard {
check(property: string): any
}
export declare type GuardConstructor = new(options: IGuardOptions) => Guard
不清楚“不工作”是什么意思,因为您没有编写任何Typescript代码,只是声明,所以我假设
test.js
应该是test.ts
来回答您的问题
您将希望在原始Javascript源代码旁边创建一个类型声明(就像您已经做的那样),但是您需要确保该结构与原始模块等效。Typescript声明表明存在多个导出,而Javascript实现只有一个导出
误差d.ts
declare class BadRequestError extends Error {
code: string;
status: number;
inner: Error;
constructor(code: string, error: Error);
}
export = BadRequestError;
索引d.ts
import {Handler} from 'express';
declare interface GuardOptions {
someProperty: string;
}
declare class Guard {
constructor(options: GuardOptions);
check(property: string): Handler;
}
declare function guardFactory(options: GuardOptions): Guard;
export = guardFactory;
test.ts
import {Request, Response} from 'express';
import guardFactory = require('./index');
const guard = guardFactory({
someProperty: 'test',
});
const middleware = guard.check('ping');
const req = {} as Request;
const res = {} as Response;
const next = function (err) {
assert.deepStrictEqual(null, err);
};
middleware(req, res, next);
您的问题可能与以下事实有关:您正在使用类型完全不同的
BadRequestError
覆盖Guard
“factory”导出。BadRequestError是一个不同的文件,有点令人困惑,如果它与您的问题无关,您能否将其删除?在哪里实现类型声明?也在一个单独的文件中?对不起,现在应该更好了。BadRequestError
可能在middleware
中返回Guard
类在哪里声明?这也是一个单独的文件吗?“不工作”并不意味着WebStorm和VS代码都没有为键入提供代码完成、语法突出显示或验证。谢谢你的耐心。