Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Derive Type from Array Typescript

I have an array

const frequencyOptions = ['Daily', 'Weekly', 'Monthly'];

I need to set Typescript Type to another array that can contain values from the options array only like

let frequencyValues = ['Daily', 'Weekly'] // can be any combination of options array
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you're controlling options array creation you can:

const frequencyOptions = ['Daily', 'Weekly', 'Monthly'] as const;
type Option = typeof frequencyOptions[number]; // "Daily" | "Weekly" | "Monthly"

let frequencyValues: Option[] = ['Daily', 'Weekly'];

// Expect error
frequencyValues = ['foo'] // Type '"foo"' is not assignable to type '"Daily" | "Weekly" | "Monthly"'.

Playground


as const prevents literal types widening to string, then we query tuple item type.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!