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

132
Visualizações
How can I narrow down a return type to a more specific type in typescript?

This came up when working with the mysql2/promise library in TypeScript.

Consider the following (abbreviated) Snippet:

import {FieldPacket, PoolConnection, RowDataPacket} from 'mysql2/promise';

let connection: PoolConnection = await promisePool.getConnection();

connection.query(FETCH_USER_ID)//returns Promise<[(RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader), FieldPacket[]]>
    .then((results: [RowDataPacket,FieldPacket[]]) => {
        fields.UserID = results[0].value;
    });

From the semantics of my query, I know for a fact that in this specific case the return type will indeed be [RowDataPacket,FieldPacket[]], but TypeScript does not allow that assignment because it is of course formally wrong.

The case where formally a number of types could apply, but semantically only one of them or a subset thereof are possible in any given situation can't be that rare, so I assume there must be a solution.

How can I tell TypeScript to expect a subset of the theoretically possible types, preferably without resorting to any or ts-ignore?

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

0

You can type cast the result using the as operator.

import {FieldPacket, PoolConnection, RowDataPacket} from 'mysql2/promise';

let connection: PoolConnection = await promisePool.getConnection();

connection.query(FETCH_USER_ID)//returns Promise<[(RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader), FieldPacket[]]>
    .then((res) => {
        const results = res as [RowDataPacket,FieldPacket[]]
        fields.UserID = results[0].value;
    });
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