WinRTError: Class not registered

478 Views Asked by At

I'm new in TypeScript. I'm getting error when trying to instantiating the class. Below is my sample code, actual code is different can't share.

 module ABC {
    export class A {    
       public execute<T>(action: string, data?: any, callerContext?: any): IAsyncResult<T> {
        // CODE::   
        var requestMessage = new Common.ClientClasses.ClientRequestMessage(); **// **ERROR- "WinRTError: Class not registered"****
        requestMessage.requestUri = actionRequest;
        requestMessage.method = "POST";
        requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
        Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
         .done((result: Common.ClientClasses.ClientResponeMessage) => {
             // CODE:
        }
        // Code::
      }
   }
}

declare module Common.ClientClasses {
    class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }

    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }

    class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any;
    }
}
1

There are 1 best solutions below

2
On

I did some improvements, should work:

module ABC {

    export class A {

        public execute<T>(action: string, data?: any, callerContext?: any) {
            var requestMessage = new Common.ClientClasses.ClientRequestMessage();
            requestMessage.method = "POST";
            requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
            Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
        }

     }
}

module Common.ClientClasses {

    export class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }

    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }

    export class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any {
            console.log("test");
        }
    }
}

Then it can be run as following:

var a = new ABC.A();
a.execute("some string");

declare module creates a definition file used for Intellisense but it doesn't provide any implementation that's why I changed your code so this fragment can work.

Also if you want to use any classes from the module, you must export them so they can be visible from outside of that module.