如何扩展passport.js模块AuthenticateOptions接口

Passport.js策略可以在身份验证调用中支持其他选项:

    passport.authenticate('azuread-openidconnect',{
      // Default passport options
      failWithError: true,successReturnToOrRedirect: '/',// Custom option supported by the azure-ad plugin
      // Type error - 'tenantIdOrName' does not exist in type 'AuthenticateOptions'
      tenantIdOrName: 'common',});

使用自定义策略支持的选项,例如上面的tenantIdOrName会导致打字错误,因为它不是护照的AuthenticateOptions接口found here的一部分,并在{{ 1}}签名here

我尝试了一些没有成功的事情

  • 模块扩充,即authenticate似乎会覆盖模块的类型,而不是扩展它们(我扩展名中未包含的所有内容都视为未键入)
  • 合并接口,即declare module 'passport' {...},这似乎对declare namespace passport { interface AuthenticateOptions { ...new properties }}方法签名没有影响。

在没有类型转换的情况下,如何在authenticate调用中支持其他属性?

i850528 回答:如何扩展passport.js模块AuthenticateOptions接口

原来,我需要导入现有模块以进行模块扩充以扩展模块的类型。

.d.ts文件*中的以下内容成功扩展了AuthenticateOptions界面:

import { AuthenticateOptions } from 'passport';

declare module 'passport' {
  // Extend acceptable authenticate options for Passport Azure AD
  // https://github.com/AzureAD/passport-azure-ad#513-options-available-for-passportauthenticate
  interface AuthenticateOptions {
    customState?: string;
    resourceURL?: string;
    tenantIdOrName?: string;
    domain_hint?: string;
    login_hint?: string;
    prompt?: string;
  }
}

*我发现文件必须 not 命名为passport.d.ts,其他任何名称都可以正常工作

本文链接:https://www.f2er.com/3164186.html

大家都在问