I am working in Typescript 2.8.1 and want to group my constants into a common file, similar to an include file in other languages. While this is straight forward, I am trying to chain some methods to work with the values and cannot seem to get it to work.
EDIT: Need a default return for simplicity and have correcte my initial problem of not returning the object.
For instance, I have the numbers 1 to 10 as defined constants:
export enum CONSTANTS {
ONE = 1, TWO, ... TEN
}
I want to be able to use these in code, such as CONSTANTS.FIVE
for the number 5, but also be able to possibly do a CONSTANTS.NEGATIVE.FIVE
to get -5.
I am trying to use chained methods, but it appears that I need to then define the original enumerations as individual methods that return a value.
export class CONSTANTS {
private Value_:number;
public constructor() {
this.Value_ = 0;
}
public ONE() {
this.Value_ = 1;
return this;
}
public TWO() {
this.Value_ = 2;
return this;
}
public NEGATIVE() {
this.Value_ = this.Value_ * -1;
return this;
}
public GetValue() {
return this.Value_; // This is the function I want to default to at the end
}
value = new CONSTANTS().ONE().NEGATIVE(); // Trying for -1
Leaving off the GetValue(); returns the object.
As an alternative, and to simplify your code you could use the negative unary operator:
export enum CONSTANTS {
ONE = 1, TWO = 2, ... TEN
}
//**//
console.log(-CONSTANTS.TWO)