使用第三方库增强 Typescript 模块

问题描述 投票:0回答:1

我有一个使用名为

ah-sequelize-plugin
的第三方插件的打字稿服务器项目。它要求我创建
config
文件来扩充/添加到其配置文件中。

这是他们的其中一个

config
文件的示例
websocket.d.ts

import { ActionheroConfigInterface } from "actionhero";
declare const namespace = "websocket";
declare module "actionhero" {
    interface ActionheroConfigInterface {
        [namespace]: ReturnType<typeof DEFAULT[typeof namespace]>;
    }
}
export declare const DEFAULT: {
    websocket: (config: ActionheroConfigInterface) => {
        enabled: boolean;
        clientUrl: string;
        clientJsPath: string;
        clientJsName: string;
        destroyClientsOnShutdown: boolean;
        server: {};
        client: {
            apiPath: string;
            cookieKey: string;
        };
    };
};
export {};

这是我尝试实现的一部分:

import { ActionheroConfigInterface } from "actionhero";

const namespace = "websocket";

declare module "actionhero" {
  export interface ActionheroConfigInterface {
    [namespace]: ReturnType<(typeof DEFAULT)[typeof namespace]>;
  }
}

export const DEFAULT = {
  [namespace]: (config: ActionheroConfigInterface) => {
    return {
      enabled: true,
      // you can pass a FQDN (like https://company.com) here or 'window.location.origin'
      clientUrl: "window.location.origin",
      // Directory to render client-side JS.
      // Path should start with "/" and will be built starting from api.config..general.paths.public
      clientJsPath: "javascript/",
      // the name of the client-side JS file to render.  Both `.js` and `.min.js` versions will be created
      // do not include the file extension
      // set to `undefined` to not render the client-side JS on boot
      clientJsName: "ActionheroWebsocketClient",
      // should the server signal clients to not reconnect when the server is shutdown/reboot
      destroyClientsOnShutdown: false,

      // websocket Server Options:
      server: {
        authorization: null,
        pathname: "/ws",
        parser: "JSON",
        transformer: "websockets",
        plugin: {},
        pingInterval: 30000,
        origins: "*",
        methods: ["GET", "HEAD", "PUT", "POST", "DELETE", "OPTIONS"],
        credentials: true,
        maxAge: "30 days",
        exposed: false,
      },

我遇到的问题是第三方

config
文件和我对它们的实现之间的类型不匹配。

具体来说,这是我收到的错误:

Subsequent property declarations must have the same type.  Property '[namespace]' must be of type '{ enabled: boolean; clientUrl: string; clientJsPath: string; clientJsName: string; destroyClientsOnShutdown: boolean; server: { authorization: any; pathname: string; parser: string; transformer: string; ... 6 more ...; exposed: boolean; }; client: { ...; }; }', but here has type '{ enabled: boolean; clientUrl: string; clientJsPath: string; clientJsName: string; destroyClientsOnShutdown: boolean; server: {}; client: { apiPath: string; cookieKey: string; }; }'

我对 Typescript 相当陌生,所以我不熟悉如何开始解决这个问题。到目前为止,我的理解是,我正在“扩展”某些特定属性,例如

server
属性包含第三方文件中未定义的子属性。不过,我认为重新声明接口后面的
declare module "actionhero"
代码将允许我“扩展”这些属性,但这显然不起作用。

这是我正在使用的插件的链接:https://github.com/actionhero/ah-sequelize-plugin

typescript sequelize.js actionhero
1个回答
0
投票

基于他们的文档

pingInterval
中没有
server
但有
timeout

      // websocket Server Options:
      server: {
        // authorization: null,
        // pathname:      '/primus',
        // parser:        'JSON',
        // transformer:   'websockets',
        // plugin:        {},
        // timeout:       35000,
        // origins:       '*',
        // methods:       ['GET','HEAD','PUT','POST','DELETE','OPTIONS'],
        // credentials:   true,
        // maxAge:        '30 days',
        // exposed:       false,
      },

所有 ping 内容都在

client
部分:

      // websocket Client Options:
      client: {
        apiPath: "/api", // the api base endpoint on your actionhero server
        // the cookie name we should use for shared authentication between WS and web connections
        cookieKey: config.web.fingerprintOptions.cookieKey,
        // reconnect:        {},
        // timeout:          10000,
        // ping:             25000,
        // pong:             10000,
        // strategy:         "online",
        // manual:           false,
        // websockets:       true,
        // network:          true,
        // transport:        {},
        // queueSize:        Infinity,
      },
© www.soinside.com 2019 - 2024. All rights reserved.