TypeScript 2.4
이제 문자열 열거 형이 있으므로 코드가 작동합니다.
enum E {
hello = "hello",
world = "world"
};
🌹
TypeScript 1.8
TypeScript 1.8부터는 문자열 리터럴 유형을 사용하여 명명 된 문자열 값 (부분적으로 열거 형이 사용되는 값)에 대해 안정적이고 안전한 환경을 제공 할 수 있습니다.
type Options = "hello" | "world";
var foo: Options;
foo = "hello"; // Okay
foo = "asdf"; // Error!
더 : https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types
레거시 지원
TypeScript의 열거 형은 숫자를 기반으로합니다.
정적 멤버가있는 클래스를 사용할 수 있습니다.
class E
{
static hello = "hello";
static world = "world";
}
당신은 또한 평범하게 갈 수 있습니다 :
var E = {
hello: "hello",
world: "world"
}
업데이트 :var test:E = E.hello; 다음과
같은 작업을 수행 할 수 있어야한다는 요구 사항에 따라 다음을 충족시킵니다.
class E
{
// boilerplate
constructor(public value:string){
}
toString(){
return this.value;
}
// values
static hello = new E("hello");
static world = new E("world");
}
// Sample usage:
var first:E = E.hello;
var second:E = E.world;
var third:E = E.hello;
console.log("First value is: "+ first);
console.log(first===third);