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 acceptedThe 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?
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.
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')[];
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With