0

Say I have the following two types:

type OrganizationType = 'NGO' | 'Business';
type Organization = {
    name: string,
    type: OrganizationType,
    weekdaysOpen: string[],
};

How can I define weekdaysOpen such that its elements are restricted to any (or none) of some predefined values? In this case they would be:

// Allowed elements in weekdaysOpen
'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'

Bonus if each allowed value can only appear once.

1

2 Answers 2

2

You can try with:

type OrganizationType = 'NGO' | 'Business';
type WeekDay = 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';
type Organization = {
    name: string,
    type: OrganizationType,
    weekdaysOpen: WeekDay[],
};
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a Set to allow only unique values:

type WeekDay = 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';

type Organization = {
  ...
  weekdaysOpen: Set<WeekDay>
};

1 Comment

Very elegant, thank you! Regardless, I'm in awe by all these quality suggestions appearing so quickly, thanks everyone!

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.