Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda