1

I'm just picking up on Typescript and I have a lot of code that I want to separate in multiple files using the same namespace: ProjectName.Validators. This namespace will hold a part of my project which handles some validations.

The validation package is composed of an interface, an exception class and the validator classes (email, regex, url etc..). Some of the validator classes are quite long and I would really like to split each one on it's own separate file. My ideal project structure would be as follows:

project/
    validators/
        interface.ts
        exception.ts
        email.ts
        url.ts
        regex.ts
        ..... extra classes in ProjectName.validators namespace
    main.ts

The problem is that my interface has one of the methods accepting as a return a void or my custom exception being thrown and I can't seem to link that exception in the code since the ts compiler is complaining about the fact that the Module ... has no exported member 'Exception' since the exception is under the same namespace in a different file.

Here's my interface definition (simplified):

/// <reference path="./exception.ts"/>

export module ProjectName.Validators {
    // Interface for all validators (builtin or custom)
    export interface Interface {
        validate(params?: Object): void|ProjectName.Validators.Exception|Error;// this is where I get the error
    }
}

and also my exception definition:

export module ProjectName.Validators {
    export class Exception extends Error {
        public name: string;
        public stack: string;

        constructor(public message?: string){
            super(message);
        }

        toString() {
            return this.name + ': ' + this.message;
        }
    }
}

As you can see I tried playing with the export keyword a lot with no success. What am I doing wrong?

Romeo Mihalcea
  • 9,714
  • 12
  • 50
  • 102

0 Answers0