I'm trying to accomplish something like following...I define a type for allowed values in array, but when I'm trying to add value to array, I get an error.
Here's the type definition:
export const SupportedFieldRules = {
REQUIRED: 'required',
NUMBER: 'number',
BOOLEAN: 'boolean'
};
export type ValidationRule = keyof typeof SupportedFieldRules;
export class FieldModel {
rules: ValidationRule[] = [];
}
And here how I want to use it:
const model = new FieldModel();
model.rules.push(SupportedFieldRules.REQUIRED);
I'm getting error for this:
Type 'string' is not assignable to type '"REQUIRED"'.
From what I understand, I have two issues here...One of them is that keys of SupportedFieldRules
are uppercase and values are in lower, and I need to find out how to create type from values of SupportedFieldRules
and not from keys (I do not want to depend on keys, only on values).
And the second issue, that I cannot push item into array, even if the keys and values of SupportedFieldRules are in the same case.
How I can solve it?
Thanks!