typescriptpassport.jspassport-azure-ad

How to extend passport.js module AuthenticateOptions interface


Passport.js strategies can support additional options in the authenticate call:

    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',
    });

Using an option that's supported by a custom strategy, eg tenantIdOrName above, results in a typescript error because it's not a part of passport's AuthenticateOptions interface found here and used in the authenticate signature here

I've tried a few things without success

How can I support additional properties in the authenticate call without type-casting?


Solution

  • Turns out I needed to import the existing module for my module augmentation to extend the module's types.

    The following in a .d.ts file* successfully extends the AuthenticateOptions interface:

    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;
      }
    }
    

    *I found the file must not be named passport.d.ts, any other name worked fine