Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function Typescript:无法在函数内创建属性_Function_Syntax_Properties_Typescript - Fatal编程技术网

Function Typescript:无法在函数内创建属性

Function Typescript:无法在函数内创建属性,function,syntax,properties,typescript,Function,Syntax,Properties,Typescript,我在我的项目中使用TypeScript,无法为任何内容分配属性。我试图从服务中获取一些数据,并将其分配给使用构造函数声明的私有对象。但是,我一直收到相同的错误:TypeError:无法在[some data here]上创建属性[some property here]。 这是我的密码: module MyModule.Controllers { "use strict"; export interface ICurrentUser { DisplayName:

我在我的项目中使用TypeScript,无法为任何内容分配属性。我试图从服务中获取一些数据,并将其分配给使用构造函数声明的私有对象。但是,我一直收到相同的错误:TypeError:无法在[some data here]上创建属性[some property here]。 这是我的密码:

module MyModule.Controllers {
    "use strict";

    export interface ICurrentUser {
        DisplayName: string;
        Features: string[];
    }

    export class DashboardController  {
        authenticationService = new Services.AuthenticationService(this.$http);
        static $inject = ["$http"];

        constructor(private $http: ng.IHttpService, private currentUser: ICurrentUser, private currentUserFeatures: string[])
        {
            this.getCurrentUser();
            this.getUserGroupForCurrentUser();
            return;
        }

        getUserGroupForCurrentUser = () => {
            this.authenticationService.getUserGroupForCurrentUser().then((authenticationId) => {
                this.currentUser.Features = authenticationFeatures;
           });
        }

       getCurrentUser = () => {
           this.authenticationService.getCurrentUser().then((user) => {
               this.currentUser.DisplayName = userName;
           });
        }

    }

}
无法在字符串“John Doe”上创建属性“DisplayName”–

这是基于以下代码:

this.currentUser.DisplayName = userName;

似乎在运行时,
this.currentUser
实际上是一个字符串
johndoe
。这表明类型声明(实际上是向编译器提示不在其控制范围内的外部系统)在运行时与实际的外部系统不匹配

需要您获取的实际错误TypeError:无法在字符串“John Doe”上创建属性“DisplayName”,我无法理解。看起来像是
this.currentUser.DisplayName=userName是问题的根源。可能
this.currentUser
不是
iccurrentUser
对象,而是其他对象,因为它在承诺中?不,它不是。接口不创建任何JavaScript,因此没有运行时强制。您仍然可以将任何类型的变量传递到构造函数中。如果您在TypeScript中尝试它,您将得到一个编译时错误,尽管它仍然可以生成JavaScript。如果在JavaScript中尝试,则不会出现错误。我猜克里斯是对的。你不小心传入了一个字符串。另外,我看不出在同一行中,
userName
是从哪里来的。@user1789573你绝对确定吗?TypeScript只在编译时检查类型,但是可能有一些写得不好的代码导致
currentUser
在运行时不是
ICurrentUser
?尝试在
this.currentUser.DisplayName=userName上放置断点
并检查这个.currentUser
真正包含的内容。