Skip to content
npmstudy edited this page Dec 13, 2023 · 1 revision

rpc.fn会在ts里报错,说类型找不到。

之前为了简单,刷了个小聪明

但是rpc.fn会在ts里报错,说类型找不到。

export function createServer(cfg?: IIndexServerConfig) {
  const rpc = new RpcServer(mergeDeep({ fn: {} }, cfg));
  const fn = new Fn(mergeDeep({}, cfg.fn));
  rpc.plugin(fn);

  return mergeDeep(rpc, {
    base: '.',
    init: rpc['config']['hooks']['init'],
    before: rpc['config']['hooks']['before'],
    load: rpc['config']['hooks']['load'],
    after: rpc['config']['hooks']['after'],
    fn: function (key, fun) {
      log('use rpc.fn add fn =' + key);
      fn.fn(key, fun);
    },
    add: function (items) {
      log('use rpc.add add fn =' + items);
      fn.add(items);
    },
  });
}

修改如下

export class FnServer extends RpcServer {
  private fnPlugin;
  public base;
  public init;
  public before;
  public load;
  public after;

  constructor(cfg?: IIndexServerConfig) {
    super(cfg);
    this.fnPlugin = new Fn(mergeDeep({}, cfg.fn));
    this.plugin(this.fnPlugin);

    this.base = '.';
    this.init = this['config']['hooks']['init'];
    this.before = this['config']['hooks']['before'];
    this.load = this['config']['hooks']['load'];
    this.after = this['config']['hooks']['after'];
  }

  public fn(key, fun) {
    log('use rpc.fn add fn =' + key);
    this.fnPlugin.fn(key, fun);
  }
  public add(items) {
    log('use rpc.add add fn =' + items);
    this.fnPlugin.add(items);
  }
}

export function createServer(cfg?: IIndexServerConfig) {
  const rpc = new FnServer(mergeDeep({ fn: {} }, cfg));
  return rpc;
}
Clone this wiki locally