Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
Conditional arguments

I would like to create a function that restricts the arguments passed. dpr and w should not be both passed at the same time. Here's my attempt:

Playground


interface W { w: number }
interface Dpr { dpr: number }

type Srcset<T> = { url: string } & WidthOrDpr<T> 

type WidthOrDpr<T> = T extends W ? W : Dpr; 

function formatSrcset<T extends W | Dpr>(options: Srcset<T>) {

}

formatSrcset({ url: '//', dpr: 1 }); // ok
formatSrcset({ url: '//', w: 1 }); // ok
formatSrcset({ url: '//', w: 1, dpr: 1 }); // should fail

I would like to avoid function overloads.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is not the most concise code, but what if you forced dpr / w to be undefined by defining two interfaces for the different arg options? Something like this:

type Args1 = { dpr: number, w?: undefined };
type Args2 = { dpr?: undefined, w: number };

type Srcset<T> = { url: string } & (Args1 | Args2);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use type-fest's RequireAtLeastOne

// code from sindresorhus/type-fest

type RequireAtLeastOne<
    ObjectType,
    KeysType extends keyof ObjectType = keyof ObjectType,
> = {
    // For each `Key` in `KeysType` make a mapped type:
    [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
        // 2. Make all other keys in `KeysType` optional
        Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
}[KeysType] &
    // 3. Add the remaining keys not in `KeysType`
    Except<ObjectType, KeysType>;

In your case

type Args = RequireAtLeastOne<{
    url: string
    dpr?: number
    w?: number
}, 'dpr' | 'w'>


function fn({ url, dpr, w }: Args) {...}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda