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

76
Visualizações
Nullable type not being applied when exporting

I have a hard time understanding why it wont apply my nullable type

Heres an example

interface Book {
  name: string;
  author: string;
  reference: string;
  category: string;
}

async function handleFetch<T>(endpoint: string, params: object): Promise<T | null> {
  const querystring = Object.entries(params)
    .map(([key, value]) => `${key}=${value}`)
    .join('&');

  try {
    const response = await fetch(`/api/${endpoint}?${querystring}`);
    const data = await response.json();

    if (response.ok) {
      return data[0] || null;
    }

    return null;
  } catch {
    throw new Error('Internal error');
  }
}

export default {
  getBook: (reference: string) => {
    return handleFetch<Book>('books', { reference });
  },
};

I can see that the method returns a nullable type enter image description here

but when exporing it isn't nullable anymore enter image description here

I feel like I am missing something but have never encountered that before

thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The only cause of this I can think of would be that the strictNullChecks compiler option is off. This is also included by the "strict" compiler option

Try adding:

"strict": true,

Or:

"strictNullChecks": true,

to your tsconfig.json file under the "compilerOptions" section.

In strict mode, your code seems to work fine. See this sandbox.

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