Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript type for an array that can contain only elements from a specific list

I want to declare a type for an array that can contain maximum one of the following strings: 'first', 'second', 'third'.

Some valid examples of how that array could be:

  • []
  • [ 'first' ]
  • [ 'first', 'second' ]
  • [ 'first', 'second', 'third' ]
  • [ 'first', 'third' ]
  • [ 'second', 'third' ]

Some invalid arrays:

  • [ 'other-value' ] // no other values should be accepted
  • [ 'first', 'first' ] // no duplicates accepted

The types I've wrote:

export enum MyOptions {
  first = 'first',
  second = 'second',
  third = 'third'
}

export type MyType = {
  name: string;
  email: string;
  listings: {
   MyOptions;
  }[];
};

It has a warning saying Member 'MyOptions' implicitly has an 'any' type, but a better type may be inferred from usage.

So if it is changed to:

export type MyType = {
  name: string;
  email: string;
  listings: {
   options: MyOptions;
  }[];
};

Now, there is no warning but it has that extra options value that I don't think it must be added.

Any ways to fix this?

like image 887
Leo Messi Avatar asked Oct 24 '25 17:10

Leo Messi


2 Answers

You can use the Set data structure.

export type Options = 'first' | 'second' | 'third'
export type Listing = Set<Options>

export type MyType = {
    name: string;
    email: string;
    listings: Listing;
};

const myType = {
  name: 'name',
  email: 'email',
  listing: new Set(['first', 'second'])
}

Then you will be sure that there are no repeated options in your listing. Note: actually you can pass repeated options but Set will ignore them.

like image 124
Ali Shabani Avatar answered Oct 26 '25 09:10

Ali Shabani


I think you have an extra set of braces that you don't need.

export type MyType = {
  name: string;
  email: string;
  listings: {
   MyOptions;
  }[];
};

should be changed to

export type MyType = {
  name: string;
  email: string;
  listings: MyOptions[];
};

You also have a more concise option: use string literal types:

export type MyType = {
  name: string;
  email: string;
  listings: ('first' | 'second' | 'third')[];
};
like image 24
Rob Lyndon Avatar answered Oct 26 '25 08:10

Rob Lyndon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!