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

143
Views
Make TypeScript understand that object properties can not be undefined?

Asking this here because not sure how to phrase it properly so google understands.

I have a utility function to capitalize words in a string, that as a second argument can get passed an options object:

export type Options = {
  onlyFirstWord?: boolean;
  separator?: string;
};

const defaultOptions = {
  onlyFirstWord: false,
  separator: " ",
};

export default function capitalize(
  str: string,
  options: Options = defaultOptions
) {}

So the second argument is optional, since I've provided a default value for it, but if I make the properties optional in the type, it requires me to check if those are undefined or not, even though they 100% can never be undefined, because of the default value. But if I don't make them optional, I will have to enter every single property when passing the options object, otherwise it would give an error.

I know there is a lot of similar questions, but in this case, is there no way to tell typescript that those values are never undefined, other than having to put exclamation marks or use optional chaining? Shouldn't it automatically figure out that they are never undefined since I'm providing a default value for it? Thanks and do let me know if this is a duplicate and this has already been answered.

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

0

Here is a playground link to a working solution.

The code is copied below, with comments:

// remove the ?'s from the Options type
type Options = {
  onlyFirstWord: boolean;
  separator: string;
};

const defaultOptions = {
  onlyFirstWord: false,
  separator: "default",
};

// use this function to merge default options with Partial options
const createDefaultOptions = (options: Partial<Options>) => {
    return {...defaultOptions, ...options};
}

// allow the capitalize function to take Partial options
function capitalize(
  str: string,
  options: Partial<Options> = defaultOptions
) {
    // merge the given options with the partial options
    const opts = createDefaultOptions(options);
    // print the result
    console.log(`${str}: ${opts.onlyFirstWord}, ${opts.separator}`);
}

// tests
capitalize('nothing');
capitalize('first', {onlyFirstWord: true});
capitalize('second', {separator: 'j'});
capitalize('both', {onlyFirstWord: true, separator: 'nice'});

Output:

nothing: false, default
first: true, default
second: false, j
both: true, nice
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!