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

106
Views
Tuple vs hard coded string

We have this code at our work, which i wanted to know the difference between it and just manually typing out the same thing.

const tuple = <T extends string[]>(...args: T) => args;
const strings = tuple(
  'blabla',
  'default',
  'standard'
);

export type Strings = typeof strings[number];

When i hover over "Strings", it basically is type Strings = 'blabla' | 'default' | 'standard'

My question is, why not just simply type out the samething?

type Strings = 'blabla' | 'default' | 'standard';

Instead of all the tuple stuff? I can't see a difference, but if someone could explain why we're using this Tuple function that would be great

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you don't export strings then it is definitely better to directly declare the type. In some cases you also need the values though, and using that tuple function saves you from having to specify each one twice.

There's a simpler and safer way to do this though:

export const strings = [ 'blabla', 'default', 'standard' ] as const;
// type: readonly ["blabla", "default", "standard"]

export type Strings = typeof strings[number]
// type Strings = "blabla" | "default" | "standard"

This makes strings readonly, so you can't do an unsound strings.pop() for example.

TypeScript playground

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!