代码之家  ›  专栏  ›  技术社区  ›  Alexander Zeitler

为原型函数添加类型

  •  0
  • Alexander Zeitler  · 技术社区  · 5 年前

    我有一个JavaScript function 在这样的节点模块中(简化):

    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)
    }
    

    error.js :

    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)
    

    用法如下:

    test.js :

    var guard = require('../index')({
      someProperty: 'test',
    })
    var req = {}
    guard.check('ping')(req, res, function (err) {
      assert.deepStrictEqual(null, err)
    })
    

    index.d.ts :

    export declare interface IGuardOptions {
        property: string
    }
    

    出口途径 class

    export declare class Guard  {
        constructor(options: IGuardOptions)
        check(required: string | Array<string>): any
    }
    

    出口途径 interface 宣言:

    export declare interface Guard {
        check(property: string): any
    }
    export declare type GuardConstructor = new(options: IGuardOptions) => Guard
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Jake Holzinger    5 年前

    不清楚“不工作”是什么意思,因为您没有编写任何类型脚本代码,只是声明,所以我假设 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);