What type to give a JSON object

Total Post:154


 1266  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. 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


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.