What type to give a JSON object

Total Post:154

 1082  View(s)
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. Post:412

    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

      Modified On Apr-04-2018 01:38:49 AM



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