Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"export as namespace" automatically in declaration file

I have a small ts library that gets outputed as UMD and I also output the *.d.ts file automatically via tsconfig: "declaration": true.

The file being exported looks like:

export class Blue { 
    alert(): void {
        console.log('alerted');
    }
}

Using exported UMD module declares a window.myLib variable.

The d.ts file looks like:

export declare class Blue {
    alert(): void;
}

Now, either via webpack, or a typescript option that I have not found I would like to also generate in the d.ts file the following line:

export as namespace myLib;

Is there a way to do this? Thanks.

like image 218
sirrocco Avatar asked Feb 06 '17 20:02

sirrocco


Video Answer


1 Answers

Instead of just:

export class Blue { 
    alert(): void {
        console.log('alerted');
    }
}

Try:

class Blue_ { 
  alert(): void {
      console.log('alerted');
  }
}

export { Blue_ as Blue };

declare global {
  namespace myLib {
    let Blue: typeof Blue_;
  }
}

This would spit the following declaration file:

declare class Blue_ {
    alert(): void;
}
export { Blue_ as Blue };
declare global {
    namespace myLib {
        let Blue: typeof Blue_;
    }
}

Now myLib.Blue is accessible globally, and also, class Bluecould be imported like import { Blue } from '...'. Which is equivalent to having export as namespace myLib; in the declaration file.

Hope this helps.

like image 65
zzxoto Avatar answered Oct 15 '22 04:10

zzxoto