简体   繁体   English

如何在Typescript中为私有类成员声明参数和返回类型?

[英]How do I declare the arguments and a return type for a private class member in typescript?

I am attempting to convert some code from vanilla javascript, and part of that requires storing a function as a private class member. 我正在尝试从原始javascript转换一些代码,并且其中一部分需要将函数存储为私有类成员。 I am attempting to figure out the proper syntax for this, but can't seem to get it right. 我正在尝试找出正确的语法,但似乎无法正确处理。

What I want to ultimately say is: 我最终要说的是:

module MyModule {
    export class MyClass {
        private myLocalReferenceToTheFunction: Function<T>(serverCall: (...args: any[]) => ng.IPromise<T>): ng.IPromise<T>;

        constructor(myOtherService: OtherService) {
           this.myLocalReferenceToTheFunction = myOtherService.serviceFunction;
        }
    }
}

What is the proper syntax for the private myLocalReferenceToTheFunction... line? private myLocalReferenceToTheFunction...行的正确语法是什么?

Tangentially, if this is the totally wrong way to go about doing this, I'd appreciate knowing. 切地,如果这是完全错误的方法,我将不胜感激。 Really, my only end goal is to be able to use myLocalReferenceToTheFunction in functions inside of MyClass . 确实,我唯一的最终目标是能够在MyClass内部的函数中使用myLocalReferenceToTheFunction

Looks to me like this is what you're trying to do: 在我看来,这就是您要执行的操作:

module MyModule {
    export class MyClass<T> {
        private myLocalReferenceToTheFunction: (serverCall: (...args: any[]) => ng.IPromise<T>) => ng.IPromise<T>;

        constructor(myOtherService: OtherService) {
            this.myLocalReferenceToTheFunction = myOtherService.serviceFunction;
        }
    }
}

Hard for me to tell for sure that this is what you need though. 我很难确定这是您所需要的。
Seeing the definition of OtherService would help. 看到OtherService的定义会有所帮助。

Something like this? 像这样吗

var MyModule = function() {
    return {
        MyClass : function () {
            var myLocalReferenceToTheFunction;
            var constructor = function(myOtherService) {
                myLocalReferenceToTheFunction = myOtherService;
            };
            return constructor;
        }()
    };
}();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM