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

182
Visualizações
Typeguard doesn't work inside await new Promise

I have a typeguard that check is authResponse exists. It works well, but inside await new Promise i have a compiler error, that object is possible null. I check type into deeplink condition and there is AuthResponse, but inside new Promise type is AuthResponse | null. Could you tell me, please, is it possible to make this type work correctly inside new Promise without optional chaining?

const hasAuthResponse = (
  response: AuthResponse | null,
): response is AuthResponse => !!response;

if (hasAuthResponse(authResponse)) {
  const deepLink = getRedirectDeepLink(
    redirect_to_scheme,
    "",
    allowedSchemas ?? undefined,
  );

  if (deepLink) {
    router.push({
      pathname: deepLink,
      query: {
        rsid: authResponse.rsid,
      },
    });

    return;
  }

  await new Promise<void>((resolve) => {
    helper
      ? helper.propagateSession(
          authResponse.rsid,
          {
            back: back ?? Urls.PROFILE,
          },
          resolve,
        )
      : resolve();
  });
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your example isn't quite complete, but I'd wager authResponse is probably not const.

Since the promise call is asynchronous, TypeScript infers that the value of authResponse could have become null by the time propagateSession gets called.

The easy fix is to rebind authResponse into a local name and use that (since it's guaranteed that its type will remain AuthResponse):

if (hasAuthResponse(authResponse)) {
    const authResponse2 = authResponse;  // inferred type: AuthResponse

but since you only really use the rsid field from the AuthResponse, you can just destructure it out of the authResponse and use it in the subsequent calls:

if (hasAuthResponse(authResponse)) {
    const {rsid} = authResponse;
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