1

I am trying to get started with some typescript. This is what I looks like:

declare module ServiceActions {
    export class MyClass{
        myFunction(shipmentId: number) : void {
            let test: number;
            test = 32;
        }
    }
}

When I do that I get this error:

An Implementation cannot be declared in ambient contexts.

When I take out the module, it works fine:

export class MyClass{
    myFunction(shipmentId: number) : void {
        let test: number;
        test = 32;
    }
}

I thought I read that Modules are like namespaces in Typescript, but this seems to make it more like an abstract/interface concept.

Am I really getting this error because of the module? If not, why am I gettin this error?

Note: I saw this question and answer and it did not answer my question.

1 Answer 1

5

In this case, you are in an "ambient" context because of the declare keyword. What you want here is to use export on your module:

export module ServiceActions {
    export class MyClass{
        myFunction(shipmentId: number) : void {
            let test: number;
            test = 32;
        }
    }
}

You could also just leave off the export altogether, which would be more like a namespace declaration. export is needed if you're using something like AMD or CommonJS or ES2015 for module declarations.

declare is used when you want to specify a module to contain things like interfaces. So perhaps:

declare module ServiceActions {
    interface MyInterface {
        myFunction(shipmentId: number) : void;
    }
}
module ServiceActions {
    export class MyClass implements MyInterface {
        myFunction(shipmentId: number) : void {
            let test: number;
            test = 32;
        }
    }
}

You'll see it a lot in .d.ts files, for example.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.