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?
Yes! You need to declare a union type for the keys:
type allowedValues = 'x' | 'y';
export interface X {
evaluation: allowedValues[];
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加