Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 930 Bytes

File metadata and controls

37 lines (29 loc) · 930 Bytes

Section 5.3: Extending enums without custom enum implementation

enum SourceEnum {
  value1 = <any>'value1',
  value2 = <any>'value2'
}
  
enum AdditionToSourceEnum {
  value3 = <any>'value3',
  value4 = <any>'value4'
}


// we need this type for TypeScript to resolve the types correctly
type TestEnumType = SourceEnum | AdditionToSourceEnum;
// and we need this value "instance" to use values
let TestEnum = Object.assign({}, SourceEnum, AdditionToSourceEnum);
// also works fine the TypeScript 2 feature
// let TestEnum = { ...SourceEnum, ...AdditionToSourceEnum };

console.log(TestEnum);

function check(test: TestEnumType) {
  return test === TestEnum.value2;
}

console.log(TestEnum.value1);
console.log(TestEnum.value2 === <any>'value2');
console.log(check(TestEnum.value2));
console.log(check(TestEnum.value3));

/******** 
 * Some how there have a bit error 
 * Object.assign not support there
 * **********/