WHAT TYPE TO GIVE A JSON OBJECT

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
C# 
C#
 886  View(s)
Ratings:
Rate this:

I have the following form

 using (Ajax.BeginRouteForm(

   ...

                new AjaxOptions

                {

                    HttpMethod = "POST",

                    OnFailure = "OnFailure",

                    OnSuccess = "OnSuccess"

                }))

            {

                ..,.

            }

I implement the OnSuccess function in a TypeScript file. I am trying to make this function more TypeScripty. At the moment I have this

function OnSuccess(data: what type goes here?) {

...

// use data.SomeValue here

...

}

Question is what type shall I say data is such that I can still somehow use data.SomeValue?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: What type to give a JSON object

    Hi Manoj,


    The best way is with an interface, because you make an explicit assertion of the structure of the object returned and communicate very clearly what the callback expects.

    export interface IOnSuccessArgs {

        propertyA: string;

        propertyB: number;

    }

    function OnSuccess(data: IOnSuccessArgs): void {

       // ... data has propertyA and propertyB

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!