Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
值的树结构:避免在Typescript中硬编码值_Typescript_Hardcode - Fatal编程技术网

值的树结构:避免在Typescript中硬编码值

值的树结构:避免在Typescript中硬编码值,typescript,hardcode,Typescript,Hardcode,我是一名Java开发人员,当我想避免直接在代码中使用硬代码值时,有时我会创建一个值树,如下所示: public class Values { public static final class Development { public static final class Environment { public static final String DEBUG = "debug"; public static fi

我是一名Java开发人员,当我想避免直接在代码中使用硬代码值时,有时我会创建一个值树,如下所示:

public class Values {

    public static final class Development {

        public static final class Environment {

            public static final String DEBUG = "debug";
            public static final String PRO_DEBUG = "pro_debug";
            public static final String TEST = "pre_release";
            public static final String AUTO_TEST = "auto_test";
            public static final String ALPHA = "alpha";
            public static final String RELEASE = "release";

        }
    }

    // [etc]
}
我最近转向了Typescript,我希望保持这种良好的做法,避免在代码中写入文字

有时我使用枚举,但现在我需要在其他值中写入值来表示json配置文件的属性


Typescript中的正确等价物是什么?

您可以在Typescript中定义类似的结构,尽管这种方法似乎不太面向Typescript:

class Values {

    public static Development = class {

        public static Environment = class {

            public static readonly DEBUG = "debug";
            public static readonly PRO_DEBUG = "pro_debug";
            public static readonly TEST = "pre_release";
            public static readonly AUTO_TEST = "auto_test";
            public static readonly ALPHA = "alpha";
            public static readonly RELEASE = "release";

        }
    }
}

Values.Development.Environment.DEBUG
如果不想使用枚举,这可能是一个更好的选择:

class Values {
    public static Development = Object.freeze({
        Environment :  Object.freeze({
            DEBUG : "debug",
            PRO_DEBUG : "pro_debug",
            TEST : "pre_release",
            AUTO_TEST : "auto_test",
            ALPHA : "alpha",
            RELEASE : "release"
        })
    })
}

Values.Development.Environment.DEBUG

您可以在Typescript中定义类似的结构,尽管这种方法似乎不太面向Typescript:

class Values {

    public static Development = class {

        public static Environment = class {

            public static readonly DEBUG = "debug";
            public static readonly PRO_DEBUG = "pro_debug";
            public static readonly TEST = "pre_release";
            public static readonly AUTO_TEST = "auto_test";
            public static readonly ALPHA = "alpha";
            public static readonly RELEASE = "release";

        }
    }
}

Values.Development.Environment.DEBUG
如果不想使用枚举,这可能是一个更好的选择:

class Values {
    public static Development = Object.freeze({
        Environment :  Object.freeze({
            DEBUG : "debug",
            PRO_DEBUG : "pro_debug",
            TEST : "pre_release",
            AUTO_TEST : "auto_test",
            ALPHA : "alpha",
            RELEASE : "release"
        })
    })
}

Values.Development.Environment.DEBUG

在类中使用类是可以的,但是我得到了lint警告,我最终使用了名称空间,并且对我有用:

export namespace Values {

  namespace Development {

      namespace Environment {

          const DEBUG = 'debug';
          const PRO_DEBUG = 'pro_debug';
          const TEST = 'pre_release';
          const AUTO_TEST = 'auto_test';
          const ALPHA = 'alpha';
          const RELEASE = 'release';

      }
  }

  // [etc]
}

在类中使用类是可以的,但是我得到了lint警告,我最终使用了名称空间,并且对我有用:

export namespace Values {

  namespace Development {

      namespace Environment {

          const DEBUG = 'debug';
          const PRO_DEBUG = 'pro_debug';
          const TEST = 'pre_release';
          const AUTO_TEST = 'auto_test';
          const ALPHA = 'alpha';
          const RELEASE = 'release';

      }
  }

  // [etc]
}