you can use in this practicular case type:
type myType = 'DEVELOPMENT' | 'PRODUCTION' | 'TEST'
class someClass {
// Any Value of ExampleClass.MODES
constructor(mode: myType ) { }
}
For more advanced scenarios check below code snippet form TypeScript documentation, where K extends keyof T is used to ensure, that only values being part of object specification are passed as function arguments:
function pluck<T, K extends keyof T>(o: T, names: K[]): T[K][] {
return names.map(n => o[n]);
}
interface Person {
name: string;
age: number;
}
let person: Person = {
name: 'Jarid',
age: 35
};
let strings: string[] = pluck(person, ['name']); // ok, string[]
Above can be part of your utility service and used universally across multiple constructors you wish to ensure types.
or even enclose values you wish to secure in separate class and use keyof directly:
class A {
prop1: 1;
prop2: 2;
}
class TestClass {
constructor(key: keyof A) {
}
}
let tc1 = new TestClass('prop1')
let tc2 = new TestClass('whoops') // Error
And as far as I understand your intention, you wish to have something more like valueof then keyof. If so, then yes, enums and types are things you should focus IMHO.