1

Consider:

enum allowedValues {'x','y'}

export interface X {
  evaluation: string[]; // TODO: how to constrain to contain only the values 'x' or 'y';
}

I tried declaring evaluation as the enum: evaluation: shownEvaluation[]; I also tried evaluation: keyof allowedValues;

Is it possible to constrain the values of an array with Typescript to a given subset of string values?

1 Answer 1

1

Yes! You need to declare a union type for the keys:

type allowedValues = 'x' | 'y';

export interface X {
    evaluation: allowedValues[];
}
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.