0

如何编写一个可以被 Visual Studio 代码智能感知检测到的 JavaScript 重载函数,以及如何记录它。

例如 jasmine 的 it() 函数如下所示。

函数它(期望:字符串,断言?:(完成:DoneFn)=>无效,超时?:数字):无效(+1重载)

4

1 回答 1

1

Jasmine 实际上并没有定义两种方法(一个重载另一个)。您在 IDE 中看到它的原因是因为输入文件有两个版本声明用于不同的可用用途。例如,下面是旧版本的definiteTyped是如何it()设置函数的:

// Type definitions for Jasmine 1.3
// ...

declare function it(expectation: string, assertion: () => void): void;
declare function it(expectation: string, assertion: (done: (err?: any) => void) => void): void;

作为参考,这里是该版本 Jasmine 中的相关代码,以表明只有一个函数处理这两个用例:

base.js(第 485-501 行)

/**
 * Creates a Jasmine spec that will be added to the current suite.
 *
 * // TODO: pending tests
 *
 * @example
 * it('should be true', function() {
 *   expect(true).toEqual(true);
 * });
 *
 * @param {String} desc description of this specification
 * @param {Function} func defines the preconditions and expectations of the spec
 */
var it = function(desc, func) {
  return jasmine.getEnv().it(desc, func);
};
if (isCommonJS) exports.it = it;

Env.js(第 151-161 行)

jasmine.Env.prototype.it = function(description, func) {
  var spec = new jasmine.Spec(this, this.currentSuite, description);
  this.currentSuite.add(spec);
  this.currentSpec = spec;

  if (func) {
    spec.runs(func);
  }

  return spec;
};
于 2018-04-28T04:31:44.013 回答