How to generate typescript interfaces/definitions for breeze entities

前端 未结 2 1376
执念已碎
执念已碎 2021-01-06 14:04

I’m new to SPA and I’m learning it with durandal and breeze. Recently I have switched my solution to Typescript and I’m wondering is there any good solution to generate type

2条回答
  •  粉色の甜心
    2021-01-06 14:16

    Below is a page you can drop in your site to generate typescript interface definitions. The page fetches the breeze metadata then iterates through all of the types and outputs a typescript interface declaration for each type. The output of this page can then be pasted in any typescript file (*.ts) or typescript definition file (*.d.ts). Enclose the results in a module declaration if you want to namespace the interfaces: declare module northwind { ... paste interfaces here... }.

    Before using the page you'll need to make one edit: change the entity manager's controller url from "api/northwind" to whatever your breeze controller's url is.

    The generated interfaces have a dependency on the Knockout.js typescript definitions which you can grab here: https://github.com/borisyankov/DefinitelyTyped/tree/master/knockout/

    Using the northwind example from learn.breezejs.com, the output of this definitions generator page would be something like this:

    export interface Employee extends breeze.Entity {
        FirstName: KnockoutObservable;
        LastName: KnockoutObservable;
    }
    

    you could then execute a query using breeze and cast the results to an array of employees like this:

    var manager = new breeze.EntityManager('api/northwind');
    
    var query = new breeze.EntityQuery()
        .from("Employees");
    
    manager.executeQuery(query).then(data => {
        // ***cast the results to a strongly typed array of Employee***
        var employees = data.results;
    }).fail(e => {
        alert(e);  
    });
    

    below is the definitions generator page- add a new html file to your project named "definitions.html", run the project and navigate to the page.

    
    
        Typescript Definition Generator
        
        
        
        
        
        
    
    
    
    
    

提交回复
热议问题