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

210
Visualizações
Avoiding inferring generics from multiple arguments in Typescript?

Consider this function:

function fn<T>(a: T, b: T[]): T {}

I'd like it to infer T from a, then check b against T. Instead, TS is inferring T from both a and b. E.g.:

fn(1, [1, 'str']);

This returns number | string. However, I want T to be inferred as number, so this would throw an error like "number | string isn't assignable to number". Is this possible?

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

0

You could provide an explicit type:

declare function fnA<T>(a: T, b: (T extends T ? T : never)[]): T
fnA<number>(1, [2, 'str'])
// Type 'string' is not assignable to type 'number'.(2322)

Or do something like:

declare function fnB<T>(a: T, b: (T extends T ? T : never)[]): T
fnB(1, [1, 'str']);
// Type 'string' is not assignable to type '1'.(2322)

This forces the compiler to have to examine the type of a to figure out the type of b.

But this seems to infer a more specific type than number, so this doesn't work:

fnB(1, [1, 2]);
// Type '2' is not assignable to type '1'.(2322)

I think this breaks some heuristics Typescript about when a type automatically widens to number or string, and when it doesn't. And it is tricky for typescript to know just how tightly to constrain that type.

It will work fine if the first argument is a wider type like number, though:

const x: number = 1
fnB(x, [1, 2]); // fine

Or you could just, again, provide the type if you don't like how it got inferred:

fnB<number>(1, [1, 2]); // fine

Playground

about 4 years ago · Juan Pablo Isaza Relatório

0

Though it's just an idea, you can maybe use HOF

declare const fn = <T,>(a: T) => (b:T): T
fn(1)([1,'str'])
// Argument of type '(string | number)[]' is not assignable to parameter of type 'number'.ts(2345)

But as @alex suggested, better to provide explicit type as long as you know it

about 4 years ago · Juan Pablo Isaza Relatório

0

There is an alternative approach:


type Primitives =
    | string
    | number
    | bigint
    | boolean
    | symbol
    | null
    | undefined

type BackwardInference<T, P=Primitives> =
    P extends any ? T extends P ? P : never : never;

declare function fn<T>(a: T, b: BackwardInference<T>[]): T

fn(1, [45]) // ok

fn('str', ['hello']) // ok

fn(42, ['str']) // expected error

Playground

Because TS infers literal type if first argument is a primitive, we can loose inference strictness a bit. It means when first will be literal 42, second argument will be expected as number and not 42

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