0

I want to have an array which can contain only specific value

for example socialService would be an array which can only contain value such as facebook, twitter, github, linkedin and few more.

The goal is to only initialise service which are passed in the array.

In typescript how Can I specify an array which can only have certain values?

1 Answer 1

2

You can achieve this using a string literal type. Example:

type SocialService = "facebook" | "twitter" | "github";
const arr: SocialService[] = [];
arr.push("github"); // Yay
arr.push("foo"); // Error
Sign up to request clarification or add additional context in comments.

Comments

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.