angulartypescriptopenlayersextendsdependency-tree

TypeScript merged and exported namespace error


I'm working at the app which using TS and OpenLayers. I'm trying to build dependency tree so into all app I put import and export statements into my *.ts files.

I've got file 'openlayers.ts' which extended base OpenLayers library. It extended for example openlayers interaction namespace.

My openlayers.ts looks like:

declare namespace ol {

    namespace interaction {
        interface someEvent {
            ...
        }

        class myExtendedClass extends ol.interaction.Pointer {
            ...
        }
    }
}

I know that ol namespace from base OpenLayers library and my extended ol namespace will be merge. TypeScript documentation clearly described how to merge namespaces.

But my problem began when I had to export my extended namespace, because it is using in my app and before export it - app didn't saw my extended namespace.

My openlayers.ts now looks like:

export declare namespace ol {

    ...
}

As you can see into my ol namespace I have a myExtendedClass which need to extend ol.iteraction.Pointer (from base OpenLayers).

But now, when I exported my namespace, app don't know what it is ol.interaction.Pointer. So I deduced that I have to import base OpenLayers into my file and my app now will detect what is ol.interaction.Pointer.

My openlayers.ts now looks like:

import ol = require('openlayers');

export declare namespace ol {

    ...
}

but now I have the errors:

  1. (TS) individual declarations in merged declaration 'ol' must be all exported or all local.
  2. At the ol.interaction.Pointer - (TS) Property 'Pointer' does not exist on type 'typeof interaction'.
  3. (TS) Import declaration conflicts with local declaration of 'ol'

I really don't know what I should to do. I just started with TS and those errors don't say me too much.

Any sugestions?


Solution

  • Ok, I found the solution.

    There were a few problems.

    1. First of them was fact that I didn't understood how my namespace extensions will be work. I really didn't know if I should void those extensions from my openlayers.ts file or from global OpenLayers reference. But I read TS documentation a few times more and I was looking for some solutions into Internet and after reading I understand that if I want to extend some namespace - I have to void those extends like it will be in original library. It's working like a normal string or collection extends wrote in C#. I haven't to void MyStringExtenstion.SomeMethod(myString). I have to void myString.SomeMethod();
    2. I haven't to import OpenLayers into my openlayers.ts file - it is the power of namespace merge - if the namespaces have the same names - its will be merge - that's all. So I removed import ol = require('openlayers');
    3. The last problem of this scenario was exporting my extended namespace. Now that I'm thinking about it, I see how silly it was, because if I merged my namespaces with global OpenLayers namespaces I don't need to exported it one more time. My namespaces were into global OpenLayers namespaces - that's all. So I changed export declare namespace ol to just declare namespace ol.

    I will leave my question and answer for person who will has a similar problem. I will let find solution much faster than I found.