Reputation: 1255
I'm just starting with Typescript converting some existing Angular code and I'm stuck at the first hurdle. I can't get the following to compile and I can't see what's wrong so hopefully someone will help me.
Errors:
Function declared a non-void return type, but has no return expression. Line 41 Col 102
Could not find symbol '$resource'. Line 49 Col 38
Code:
/// <reference path="../dt/angular/angular.d.ts" />
/// <reference path="../dt/angular/angular-resource.d.ts" />
var chartAPI = angular.module('chartAPI', ['ngResource', 'ngCookies']);
// Config
chartAPI.config(['$resourceProvider', '$locationProvider', '$httpProvider', function ($resourceProvider, $locationProvider, $httpProvider) {
// Don't strip trailing slashes from calculated URLs
$resourceProvider.defaults.stripTrailingSlashes = false;
// Enable HTML5 - needs HTML5 doc
//$locationProvider.html5Mode(true);
// Ensure CSRF token is sent in $http requests
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.withCredentials = true;
}]);
interface IDRFPagedResult extends ng.resource.IResource<IDRFPagedResult> {
count: number;
next: string;
previous: string;
results: Array<any>;
}
interface IViewAsUser extends ng.resource.IResource<IViewAsUser> {
id: number;
first_name: string;
last_name: string;
email: string;
}
interface IViewAsUserList extends IDRFPagedResult {
results: Array<IViewAsUser>;
}
interface IViewAsUserListResource extends IViewAsUserList {
list() : IViewAsUserList;
}
chartAPI.factory('ViewAsUserList', ['$resource', function ($resource: ng.resource.IResourceService): IViewAsUserListResource => {
var queryAction: ng.resource.IActionDescriptor = {
method: 'GET',
params: {},
isArray: false
};
return <IViewAsUserListResource> $resource('/rest/v1/view-as-user/', {}, {
list: queryAction
});
}]);
Upvotes: 0
Views: 1881
Reputation: 123861
I would also suggest to take a look at this article
To see, that to declare our resource we need a bit different interfaces structure. This should be some item
interface IDRFPagedResult {
id: number;
first_name: string;
last_name: string;
email: string;
}
Then we define: IDRFPagedResourceDef
- a simple interface that only extends a resource on IDRFPagedResult
(see mainly IResource<>
):
interface IDRFPagedResourceDef extends ng.resource.IResource<IDRFPagedResult> {
}
And finally Resource Class IDRFPagedResource
on the resource definition IDRFPagedResourceDef
(see mainly IResourceClass<>
)
interface IDRFPagedResource extends ng.resource.IResourceClass<IDRFPagedResource> {
}
Finally, the cited resource factory definition:
export class ResourceBuilder{
static $inject = ['$resource'];
constructor(private $resource: ng.resource.IResourceService) {
}
public getDRFPagedResource(): IDRFPagedResource {
return this.$resource('/rest/v1/view-as-user/', {}, {
// extend here, e.g.
// list: queryAction
});
}
}
Would suggest to read that article to see that all in action
Upvotes: 0
Reputation: 220944
You have a syntax problem on this line:
chartAPI.factory('ViewAsUserList', ['$resource', function ($resource: ng.resource.IResourceService): IViewAsUserListResource => {
Specifically this part:
function ($resource: ng.resource.IResourceService): IViewAsUserListResource => {
You cannot combine function
with =>
like this -- either write function (args) { ... }
or (args) => { ... }
. The compiler is trying to interpret the body of the function as part of a function type literal.
Upvotes: 2