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

95
Visualizações
Typescript 4.4 breaking tuple union inference
// Works fine in 4.3.5, not in 4.4.2

type ArgsTupleUnion<T> =
  | [obj: T, x: (arg: T) => void]
  | [y: (arg: number) => void];

function f<T>(...args: ArgsTupleUnion<T>) {}

f({ k: 15 }, (arg) => { // 4.3.5: arg is {k: number}, 4.4.2: arg is 'any'
  console.log(arg.k);
});

f((a: number) => {});


type ArgsTuple<T> =
  | [obj: T, x: (arg: T) => void]
  // | [y: (arg: number) => void];

function f2<T>(...args: ArgsTuple<T>) {}

f2({ k: 15 }, (arg) => { // Both 4.3.5 and 4.4.2: arg is {k: number}
  console.log(arg.k);
});

Here's the code on TS Playground. I'm not sure which change has made this stop working but it's preventing me from upgrading to 4.4. Anyone else who has run into this or has an idea for a workaround?

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

0

A possible work around would be to use functional overload

It seems that typescript is opting in for the more specific type number rather than tracking the number of elements in a given array.

type ArgsTuple<T> = [obj: T, x: (arg: T) => void];
type NumArrFun = [y: (arg: number) => void];

type ArgsTupleUnion<T> =
  | ArgsTuple<T>
  | NumArrFun;

function f<T>(...args: ArgsTuple<T>): void
function f<T>(...args: NumArrFun): void
function f<T>(...args: ArgsTupleUnion<T>) {}

f({ k: 15 }, (arg) => { // 4.3.5: arg is {k: number}, 4.4.2: arg is 'any'
  console.log(arg.k);
});

f((a: number) => {});

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