Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breeze entities with typescript

I'm using Breeze + Typescript + Knockout for a Spa, and I'm facing the following problem: when I create a new entity with EntityManager.createEntity, typescript doesn't let me use the observables that Breeze generates from metadata. Typescript "sees" only the "entityAspect" and the "entityType" properties. I'm using the type definitions of DefinitelyTyped. Any help is greatly appreciated!

like image 822
frenchfaso Avatar asked Feb 18 '23 02:02

frenchfaso


2 Answers

You can create an interface for your type which extends breeze.Entity:

/// <reference path="breeze.d.ts" />
module model {
    export interface ResponsesItem extends breeze.Entity {
        ContentTypeID: string;
        Title: string;
        Description: string;
        EventDate: any;
        /* etc. */
    }
}

You can then cast your objects to this interface whenever you need to work with them in a typed way, such as the result of a query when loading from the server :

     private loadResponses(): void {
         this.dataservice.ListResponses()
            .then((data: { results: breeze.Entity[]; query: breeze.EntityQuery; XHR: XMLHttpRequest; }) => {
                var results: model.ResponsesItem[] = <model.ResponsesItem[]>data.results;
                // Do something with typed results array here.
         }).fail((error) => {
            this.handleDataError(error);
         });
      }
like image 177
Jude Fisher Avatar answered Feb 23 '23 07:02

Jude Fisher


Check out my answer at Breeze.js typed entities wherein I hacked T4TS to spit out TS definitions that support Breeze.

like image 30
Alex Dresko Avatar answered Feb 23 '23 06:02

Alex Dresko