简体   繁体   English

您可以在TypeScript的本地方法中访问本地静态变量吗?

[英]Can you access local static variables within local method in TypeScript?

Lets say I have a class like below: 可以说我有一个像下面这样的课程:

class Types
{
    static TypeOne = 1;
    static TypeTwo = 2;
    static TypeThree = 3;
    static TypeFour = 4;
    static TypeFive = 5;

    public GetNameFromType(type: number) : string
    {
        switch (type)
        {
            case Types.TypeOne: return "TypeOne";
            case Types.TypeTwo: return "TypeTwo";
            case Types.TypeThree: return "TypeThree";
            case Types.TypeFour: return "TypeFour";
            case Types.TypeFive: return "TypeFive";
            default: return "Unknown";
        }
    }
}

Now after reading some documentation on static classes it seems like the above should work. 现在,在阅读了有关静态类的一些文档之后,以上内容似乎应该可以工作了。 however I keep getting an error saying that Types.TypeOne does not exist in the current scope. 但是我总是收到错误消息,说Types.TypeOne在当前作用域中不存在。

So do I need to do anything else or should this just work? 那么,我是否需要做其他事情还是应该工作?

== Edit == ==编辑==

I didnt think it was outputting anything for it due to the errors, but it appears it has, here is the output: 我不认为由于错误,它没有为它输出任何东西,但是看起来它有,这是输出:

var Types = (function () {
            function Types() { }
            Types.TypeOne = 1;
            Types.TypeTwo = 2;
            Types.TypeThree = 3;
            Types.TypeFour = 4;
            Types.TypeFive = 5;
            Types.TypeSix = 6;
            Types.prototype.GetNameFromType = function (type) {
                switch(type) {
                    case AbilityTypes.TypeOne:
                        return "TypeOne";
                    case AbilityTypes.TypeTwo:
                        return "TypeTwo";
                    case AbilityTypes.TypeThree:
                        return "TypeThree";
                    case AbilityTypes.TypeFour:
                        return "TypeFour";
                    case AbilityTypes.TypeFive:
                        return "TypeFive";
                    case AbilityTypes.TypeSix:
                        return "TypeSix";
                    default:
                        return "Unknown";
                }
            };
            return Types;
        })();     

This looks legit, so maybe it is working and is just complaining... 这看起来合法,所以也许它正在工作并且只是在抱怨...

Are you using Typescript 0.8.3? 您正在使用Typescript 0.8.3吗? Your code compiles fine for me. 您的代码对我来说很好。 If the error is happening else where, try and define them as a 'public static' 如果错误发生在其他地方,请尝试将其定义为“公共静态”

Pasting that code into the Playground ( http://www.typescriptlang.org/playground ) shows no errors or warnings for me, and this is running the same 0.8.3 bits as the MSI. 将该代码粘贴到Playground( http://www.typescriptlang.org/playground )中对我来说没有任何错误或警告,并且它与MSI的运行速度相同,为0.8.3位。 I would uninstall/reinstall if you are seeing errors with just this code, as unless this is part of a larger project and the error is occurring elsewhere, then seems like something in the install must be out of whack. 如果您仅通过此代码看到错误,我将卸载/重新安装,因为除非这是一个较大的项目的一部分并且该错误发生在其他地方,那么似乎安装中的某些内容一定不可行。

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

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