3

I try to use typescript module augmentation in order to change this class in a module dependency :

// module/token_response.ts

export class TokenResponse {
  accessToken: string;

  constructor(response: TokenResponseJson) {
    this.accessToken = response.access_token;
  }

I write "module augmentation" like this :

// my_token_response.ts

import { TokenResponse } from 'module/token_response';

declare module 'module/token_response' {
    interface TokenResponse {
        refreshExpiresIn: string | undefined;
    }
}

TokenResponse.prototype.constructor = function (response: TokenResponseJson) {
    TokenResponse.call(this, response);
    this.refreshExpiresIn= response.refresh_expires_in;
};

And then use it in a test :

import {TokenResponse} from 'module/token_response';
import 'my_token_response';


describe("Tests", function () {
    it('Test module augmentation', () => {
        let response = new TokenResponse({
            access_token: 'TOKEN',
            refresh_expires_in: '1800'
        });

        expect(response).not.to.be.null;
        expect(response.accessToken).to.be.equal('TOKEN');
        expect(response.refreshExpiresIn).to.be.equal('1800');
    });
});

Redefined constructor is never use, response.refreshExpiresIn is always undefined.

Is it possible to do this ? and how ?

vdeygas
  • 41
  • 2

0 Answers0