首页 > 解决方案 > 如何覆盖 Typescript 中的参数

问题描述

我想为我的班级提供多个接口。

例子:

type OAuthCliStrategyOpts = {
  oauthStrategy: express.Handler;
  oauthStrategyOptions: object;
  appKey: string;
  appSecret: string;
};

type OAuthCliDefaultOpts = {
  authorizationURL: string;
  tokenURL: string;
  appKey: string;
  appSecret: string;

  callbackURL?: string;
};

type OAuthCliOpts = OAuthCliDefaultOpts | OAuthCliStrategyOpts;

但是当我尝试指定时:

oauthCLI({
        oauthStrategy: DropboxOAuth2Strategy,
        oauthStrategyOptions: {
          apiVersion: "2"
        },
        appSecret: "KEY",
        appKey: "SECRET"
      })

我收到一个错误:

Argument of type '{ oauthStrategy: any; oauthStrategyOptions: { apiVersion: string; }; appSecret: string; appKey: string; }' is not assignable to parameter of type 'OAuthCliOpts'.
      Object literal may only specify known properties, and 'oauthStrategy' does not exist in type 'OAuthCliOpts'.

    146         oauthStrategy: DropboxOAuth2Strategy,
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ```

标签: typescript

解决方案


尝试这个

oauthCLI({ oauthStrategy: DropboxOAuth2Strategy, oauthStrategyOptions: { apiVersion: "2" }, appSecret: "KEY", appKey: "SECRET" } as OAuthCliStrategyOpts)


推荐阅读