Created
March 7, 2017 15:08
-
-
Save slovely/c5e3644cffbd76e85e1276043830d0f7 to your computer and use it in GitHub Desktop.
Generated WebAPI actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume a WebAPI action on the server | |
// [HttpPost] | |
// public SomeType TheAction(string s, AnotherType obj) { | |
// return new SomeType(s.Length, obj.name); | |
// } | |
// Where SomeType is | |
// public class SomeType{ public int Property1{get;set;} public string Property2{get;set;}} | |
// and AnotherType is | |
// public class AnotherType {public string Name{get;set;}} | |
// Call the server... | |
Api.myController.theAction("Hello, world", { Name: "It's me" }) | |
.done(response => { | |
// In here response will be typed to SomeType | |
const num: number = response.Property1; // VALID | |
const num2: number = response.Property2; // INVALID - Property2 on the server is a string | |
}); | |
// Works because the following TypeScript is generated: | |
declare interface SomeType{ | |
Property1: number; | |
Property2: string; | |
} | |
declare interface SomeType{ | |
Name: string; | |
} | |
// And we generate stubs that can call the server... | |
module Api { | |
export class myController { | |
public static theAction(s: number, obj: AnotherType): JQueryPromise<SomeType> { | |
return ServiceCaller.post("/mycontroller/theAction", s, obj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info here: https://github.com/slovely/TypeScriptDefinitionsGenerator