0

I have this object in TypeScript:

validationRules: ValidationRuleInterface = {
  id: ['integer', 'required'],
  name: ['string', 'required', 'people_name'],
  city: ['string', 'nullable'],
};

I want to define somehow acceptable values in arrays. Something like this:

type Rule = string<'required' | 'nullable' | 'string' | 'integer' | 'people_name', null>;

interface ValidationRuleInterface {
  [key: string]: Rule[]
}

Is there any way to define possible values of an array in TypeScript?

1 Answer 1

7

You were almost there. Simply, change the type definition to

type Rule = 'required' | 'nullable' | 'string' | 'integer' | 'people_name' | null;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.