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

139
Visualizações
Typescript inference error - variable with type string changes to undefined inside for loop

I'm using await-to-js library for error handling (the to method in the example below comes from the library)

For some reason, the type of a variable changes to string | undefined inside of a for..of loop, when the value of the same variable is string outside of the loop.

Consider the following example (see testMethod for error):

function to<T, U = Error>(
    promise: Promise<T>,
    errorExt?: object
): Promise<[U, undefined] | [null, T]> {
    return promise
        .then<[null, T]>((data: T) => [null, data])
        .catch<[U, undefined]>((err: U) => {
            if (errorExt) {
                const parsedError = Object.assign({}, err, errorExt);
                return [parsedError, undefined];
            }

            return [err, undefined];
        });
}


async function retrieveAccessToken(): Promise<string> {
    const randomNumber = Math.random();

    if(randomNumber < 0.5) {
        throw new Error("Failed");
    }

    return "testToken";
}

    
function printAccessToken(accessToken: string) {
    console.log(accessToken);
};

async function testMethod(): Promise<boolean> {
    const accessTokenPromise = retrieveAccessToken();

    const [err, accessToken] = await to(accessTokenPromise);

    if(err){
        console.log("Failed");
        return false;
    }

    // No error here
    printAccessToken(accessToken);

    for(let i = 0 ; i < 5; i++){
        // Error! Type Argument of type 'string | undefined' is not assignable to 
        // parameter of type 'string'.
        printAccessToken(accessToken);
    }

    return true;
}

This seems to be solved by adding a if check on accessToken e.g. if(!accessToken) however it doesn't make sense why the type of accessToken is string | undefined inside of the for loop but string outside of it?

Typescript playground

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

0

TypeScript has issues analyzing types in destructuring syntax. To get the expected behavior, assign the resolved value of to to a local variable, and reference accessToken after checking err.

Change:

    const [err, accessToken] = await to(accessTokenPromise);

    if(err){
        console.log("Failed");
        return false;
    }

Into:

    const toResult = await to(accessTokenPromise);

    const [err] = toResult;
    if(err){
        console.log("Failed");
        return false;
    }
    const [, accessToken] = toResult;

Playground link

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