I need to create a list of constants, e.g.,
const Days = {
YESTERDAY: -1,
TODAY: 0,
TOMORROW: 1,
}
I would like a way to constrain the types of all properties in Days, that is, if I add a property BAD_DAY: true, I would like the compiler to complain.
I started by creating something like
type ObjectOf<T> = {[k: string]: T};
const Days: ObjectOf<number> = {
A: true;
}
Which does give me an error, however, I don't get type completion when I hit ctrl-space after Days.. I understand it's because I made it a keyed object type.
I can either get code completion as in the first example, or compiler help with the second example. Can I have my cake and eat it too? I guess I want something that behaves like Java Enums in the sense that it's a known list of objects of the same type.
enum Days{
Today("SUN"), MONDAY("MON"), TUESDAY("TUES"), WEDNESDAY("WED"),
THURSDAY("THURS"), FRIDAY("FRI"), SATURDAY("SAT");
private String abbreviation;
public String getAbbreviation() {
return this.abbreviation;
}
Days(String abbreviation) {
this.abbreviation = abbreviation;
}
}