Union types

We are not quite finished with the declaration file for the ErrorHelper class just yet. If we take a look at the original JavaScript usage of the ErrorHelper class, we will notice that the containsErrors function also allows for the failure property of responseText to be a string:

    var failureMessage = { 
        responseText : { 
            "failure" : "true", 
            "errorMessage" : "Error Message from Typescript" 
        } 
    }  

If we compile this code now, we will get the following compile error:

In the preceding definition of the failureMessageString variable, the type of the "failure" property is "true", which is of type string , and not true, which is of type boolean. In order to allow for this variant on the original IFailureMessage interface, we will need to modify our declaration file. The simplest way to allow for both types would be to use a type union, as follows:

interface IFailureMessage { 
    failure: boolean | string; 
    errorMessage: string; 
} 

Here, we have simply updated the failure property on the IFailureMessage interface to allow for both boolean and string types. Our code will now compile correctly.

This completes our definition file for the ErrorHelper JavaScript class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.193.172