2

I am using typescript with atom version 1.7.5

I have a declaration file Provision.d.ts with the following declarations

declare module Provision {

export enum ProvisionMode {
    NOOP,
    PRODUCTION,
    DEVELOPMENT,
    TEST,
    DEFAULT
}

export interface ProvisionSettingsService {
    setGlobalProvisionMode(arg0: ProvisionMode, arg1: string, back: Http.HttpDefaultCallback): void;
    getGlobalProvisionMode(arg0: string, back: Http.HttpDefaultCallback): void;
    setPathProvision(arg0: string, arg1: ProvisionMode, back: Http.HttpDefaultCallback): void;
    getPathProvision(arg0: string, back: Http.HttpDefaultCallback): void;
}

}

and then my implementation file Provision.ts

/// <reference path='./Provision.d.ts'/>
module ProvisionImpl{

export class ProvisionServiceCallback implements Http.Callback<Provision.ProvisionMode>{

onSuccess(data: Provision.ProvisionMode): void {

}

onError(): void {
    var console: Console;
    console.log("provision callback Error");
}

}

export class ProvisionServiceClient implements Provision.ProvisionSettingsService{

 setGlobalProvisionMode(arg0: Provision.ProvisionMode, arg1: string, back : Http.HttpDefaultCallback): void{
      /// ..... various implementations here
 }

 getGlobalProvisionMode(arg0: string , back : Http.HttpDefaultCallback): void{
                /// ..... various implementations here
 }

 setPathProvision(arg0: string, arg1: Provision.ProvisionMode,  back : Http.HttpDefaultCallback): void{
                 /// ..... various implementations here
 }

 getPathProvision(arg0: string,  back : Http.HttpDefaultCallback): void{
      /// ..... various implementations here
 }
 }
 }

When I try to use the above implementation in my code say CountryService.ts

/// <reference path='./Provision.d.ts' />
/// <reference path='./Provision.ts' />

class App{
    public switchOp() {

        var client = new ProvisionImpl.ProvisionServiceClient();

        var noop = Provision.ProvisionMode.NOOP ;
        var prod = Provision.ProvisionMode.PRODUCTION ;

        if (this.op){
            client.setGlobalProvisionMode(noop , "dummy" , new Http.HttpDefaultCallback()) ;
        }else{
            client.setGlobalProvisionMode(prod , "dummy" , new Http.HttpDefaultCallback()) ;
        }
    }
}

new App().switchOp();

Though my code compiles without any complaint it consistently gives me an error

Uncaught ReferenceError: Provision is not defined

on this line

var noop = Provision.ProvisionMode.NOOP ; 
2
  • can you try it with export module, check the javascript generated.. is it creating a variable named Provision and creating the enum on that variable?? Commented Mar 13, 2016 at 11:55
  • Ambient declarations (*.d.ts) are mainly meant to declare things that exist in your JS, not actually supply them. Change it to a .ts source file and I believe it will work. Commented Mar 13, 2016 at 13:38

2 Answers 2

3

When you use the declare keyword, you tell the compiler "Be sure there will be some object at runtime that is called X". This is especially useful when working with JavaScript code, for example when requiring some libs like jquery. So what you are doing here, is telling the compiler that there will be an object Provision with an enum field, but you never really create such an object that is there at runtime.

Please remove the declare keyword, so that the compiler will generate the objects for you.

EDIT: Additionally, as mentioned by @Gautam, the file must not be named *.d.ts but *.ts for the TypeScript compiler to not treat it as a "declaration only" file.

See the difference in this snippet at the TypeScript Playground

Also be aware about const vs. non-const enums which behave differently. Const enums are inlined, so that you will only find some numeric values (0,1,2,..) instead of ProvisionMode.PRODUCTION in the resulting code. This is no issue with the way you declared it above.

For further details please see this great answer: How do the different enum variants work in TypeScript?

Sign up to request clarification or add additional context in comments.

2 Comments

yes. I updated my code with a declaration with export in my .d.ts file and a definition without in my .ts file.. seems to work
Can you update the answer with the extra detail I added in the other answer and I will select yours ?
1

The real answer is above - This one is a quicker / simpler version

It should be defined in a .ts file.

module Provision {

enum ProvisionMode {
    NOOP,
    PRODUCTION,
    DEVELOPMENT,
    TEST,
    DEFAULT
}

export {ProvisionMode} ;

export interface ProvisionSettingsService {
    setGlobalProvisionMode(arg0: ProvisionMode, arg1: string, back: Http.HttpDefaultCallback): void;
    getGlobalProvisionMode(arg0: string, back: Http.HttpDefaultCallback): void;
    setPathProvision(arg0: string, arg1: ProvisionMode, back: Http.HttpDefaultCallback): void;
    getPathProvision(arg0: string, back: Http.HttpDefaultCallback): void;
}

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.