2

Im trying to make a global namespace/function my code looks like this : abc.ts

declare namespace abc {
   export function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}
export = abc

What am I doing wrong? How do I fix it ?

lanzchilz
  • 149
  • 1
  • 4
  • 8

1 Answers1

4

Your code will work fine if you remove "declare" keyword

namespace abc {
   export function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}
export default abc

declare is used to tell TypeScript that the variable has been created elsewhere. If you use declare, nothing is added to the JavaScript that is generated - it is simply a hint to the compiler.

What does 'declare' do in 'export declare class Actions'?


The implementation can be in the another place in your case. The code below works fine too.

declare namespace abc {
   export function abc ();
}

namespace abc {
   function abc (): xyz {
        console.log('Hello');
        return xyz(200);
    }
}

export default abc

https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html

Dimsa
  • 272
  • 4
  • 13