javascript - How to get typescript to recognize a custom action on the angular $resource? -
this sample use of angular $resource
in typescript. in example i'm attempting create update action on resource i've defined, it's done in https://docs.angularjs.org/api/ngresource/service/$resource. i'm returning instance ng.resource.iresourceclass<ivenueresource>
module app.common { interface ivenueservice{ } export interface ivenueresource extends ng.resource.iresource<app.domain.ivenue>{ update(ivenue: app.domain.ivenue) : app.domain.ivenue; } export class venueservice implements ivenueservice{ static $inject = ["$resource", "url"]; constructor(private $resource: ng.resource.iresourceservice, private url: url_constants){ } getvenueresource(): ng.resource.iresourceclass<ivenueresource>{ // define custom action iactiondescriptor var updateaction : ng.resource.iactiondescriptor = { method: 'put', isarray: false }; return <ng.resource.iresourceclass<ivenueresource>> this.$resource(this.url.base_api+"/v1/venues/:id", {id: '@id'} ,{ update: updateaction }); } } angular .module("common.services") .service("venueservice", venueservice); }
now time use resource, resource defined interface implemented class. yet still error, how typing dictionary recognize update method.
module app.venue { interface inavigationscope{ venueresource: ng.resource.iresourceclass<app.common.ivenueresource>; } class venuelistctrl implements inavigationscope { venueresource: ng.resource.iresourceclass<app.common.ivenueresource>; } sample(){ var updatedvenue =this.venueresource.update({id: 1, name: 'amphitheather'}, ()=> {}); } }
soon realized reference https://github.com/definitelytyped/definitelytyped/blob/master/angularjs/angular-resource.d.ts#l85
i should using iresourceclass<iresource<t>>
extend interface, because iresource<t>
not have option add custom method.
interface iresourceclass<t> { new(dataorparams? : any) : t; get(): t; ... }
furthermore, when using custom method, needed assign correct declaration.
interface inavigationscope{ venueresource: app.common.ivenueresource; }
returning instance of ivenueresourceclass
child of ng.resource.iresourceclass<ng.resource.iresource<app.domain.ivenue>>
update()
method.
Comments
Post a Comment