相关文章推荐
憨厚的手电筒  ·  聚焦新闻-评论·  3 月前    · 
冷冷的草稿本  ·  智能制造工程·  1 年前    · 
风度翩翩的斑马  ·  影评 | ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to run gulp command and it gives me the following error:

src\core\services\NetworkService.ts(36,21): error TS2322: Type '{ 'Authorization': string; }' is not assignable to type 'IHttpRequestConfigHeaders'.
  Index signature is missing in type '{ 'Authorization': string; }'.
{ TypeScript error: src\core\services\NetworkService.ts(36,21): error TS2322: Type '{ 'Authorization': string; }' is not assignable to type 'IHttpRequestConfigHeaders'.

NetwrokService.ts

/// <reference path="../../../typings/app.d.ts" />
/// <reference path="../../../typings/tsd.d.ts" />
module Core.Services {
    export class NetworkService {
        static $inject: Array<string> = ['$http', '$log', '$q', 'appConstant', 'storageService'];
        constructor(public $http: ng.IHttpService, public $log: ng.ILogService, public $q: ng.IQService,
            public appConstant: Shared.AppConstants, public storageService: Services.StorageService) { }
        private onError(error: any): void {
            // generic handling for all error, including authorization realted stuff
            this.$log.error(error);
        private getConfig(url: string, config?: ng.IRequestShortcutConfig): ng.IRequestConfig {
            var httpConfig = <ng.IRequestConfig>{};
            if (config != null) {
                angular.extend(httpConfig, config);
            var token = this.storageService.getItem(this.appConstant.keys.token, false);
            if (token != null) {
                var tokenHeader = {
                    'Authorization': "Bearer " + token
                var currentHeaders = httpConfig.headers;
                if (currentHeaders) {
                    httpConfig.headers = angular.extend(currentHeaders, tokenHeader);
                } else {
                    httpConfig.headers = tokenHeader;
            httpConfig.url = url;
            return httpConfig;
        private getOrDelete<T>(url: string, methodType: string, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = methodType;
            return this.getResponse<T>(httpConfig);
        private getResponse<T>(config: ng.IRequestConfig): ng.IPromise<T> {
            var deferred = this.$q.defer();
            config.headers
            this.$http(config).success(
                (result: any) => {
                    deferred.resolve(result);
                }).error((error, errorCode) => {
                    this.onError(error);
                    deferred.reject(new Core.Models.HttpError(error, errorCode));
            return deferred.promise;
        get<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = "GET";
            if (data) {
                httpConfig.params = data;
            return this.getResponse(httpConfig);
        delete<T>(url: string, data?: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = "DELETE";
            if (data) {
                httpConfig.params = data;
            return this.getResponse(httpConfig);
        post<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = "POST";
            httpConfig.data = jQuery.param(data);
            httpConfig.headers = {
                'Content-Type': 'application/x-www-form-urlencoded'
            return this.getResponse<T>(httpConfig);
        put<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = "PUT";
            httpConfig.data = data;
            return this.getResponse<T>(httpConfig);
        patch<T>(url: string, data: any, config?: ng.IRequestShortcutConfig): ng.IPromise<T> {
            var httpConfig = this.getConfig(url, config);
            httpConfig.method = "PATCH";
            httpConfig.data = data;
            return this.getResponse<T>(httpConfig);

I have installed every dependency , but how can I remove this error, should I install any more libraries? or should I remove anything to make it work?

From the last comment, it says you need to first convert it to the string which is right so all you need to do is:

import { Headers } from '@angular/http';

and where you need the tokenHeader do

let tokenHeader =  new Headers();

and then follow Afterwards with:

tokenHeader.append('Authorization', 'Bearer'+ this.token);

This should clear the error message.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.