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 ;
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?
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.