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

Damian Green

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?

casieber

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

type allowedValues = 'x' | 'y';

export interface X {
    evaluation: allowedValues[];
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Filter dataframe by values being subset of a given set

分類Dev

Typescript enum values as array

分類Dev

change object property to match values in given array

分類Dev

Paste values from array into string

分類Dev

How to check if a DB array matches any values of a given array

分類Dev

Numpy indexing: Set values of an array given by conditions in different array

分類Dev

subset df on specified values and preceding values

分類Dev

Subset columns with NaN values in Pandas

分類Dev

Subset a distance matrix in R by values

分類Dev

Fill in values between given indices of 2d numpy array

分類Dev

PHP get possible string combination of given array which match with given string

分類Dev

Get every possible combinations of an array with fixed sum of values and array model

分類Dev

How to check character values on a String array?

分類Dev

Replace array values by another one in a string

分類Dev

How to put values of objects from an array into a string

分類Dev

Is it possible to Dim a variable in VB.net using values from an array?

分類Dev

How to make typescript to allow only the values from the array?

分類Dev

Best way to map a string to an array depending on other array values

分類Dev

Selecting a subset of rows that exceed a percentage of total values

分類Dev

Randomly assign values to subset of rows in pandas dataframe

分類Dev

Subset a dataset to leave the largest 2 values

分類Dev

Setting values given two column

分類Dev

Get values of given coordinates in Matlab

分類Dev

Given an array, use a function to sum up certain values depending on a different value in the array

分類Dev

Match string with values from array in express-validation

分類Dev

How do I ORDER BY a JSON Array with its String Values?

分類Dev

splitting string in a file and saving the values in an array is printing wrong output

分類Dev

Find Numeric Values among string from Array of Objects and do calculation

分類Dev

Distinct values from array based on a part of the string in JS

Related 関連記事

  1. 1

    Filter dataframe by values being subset of a given set

  2. 2

    Typescript enum values as array

  3. 3

    change object property to match values in given array

  4. 4

    Paste values from array into string

  5. 5

    How to check if a DB array matches any values of a given array

  6. 6

    Numpy indexing: Set values of an array given by conditions in different array

  7. 7

    subset df on specified values and preceding values

  8. 8

    Subset columns with NaN values in Pandas

  9. 9

    Subset a distance matrix in R by values

  10. 10

    Fill in values between given indices of 2d numpy array

  11. 11

    PHP get possible string combination of given array which match with given string

  12. 12

    Get every possible combinations of an array with fixed sum of values and array model

  13. 13

    How to check character values on a String array?

  14. 14

    Replace array values by another one in a string

  15. 15

    How to put values of objects from an array into a string

  16. 16

    Is it possible to Dim a variable in VB.net using values from an array?

  17. 17

    How to make typescript to allow only the values from the array?

  18. 18

    Best way to map a string to an array depending on other array values

  19. 19

    Selecting a subset of rows that exceed a percentage of total values

  20. 20

    Randomly assign values to subset of rows in pandas dataframe

  21. 21

    Subset a dataset to leave the largest 2 values

  22. 22

    Setting values given two column

  23. 23

    Get values of given coordinates in Matlab

  24. 24

    Given an array, use a function to sum up certain values depending on a different value in the array

  25. 25

    Match string with values from array in express-validation

  26. 26

    How do I ORDER BY a JSON Array with its String Values?

  27. 27

    splitting string in a file and saving the values in an array is printing wrong output

  28. 28

    Find Numeric Values among string from Array of Objects and do calculation

  29. 29

    Distinct values from array based on a part of the string in JS

ホットタグ

アーカイブ