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

121
Vistas
typescript array of objects with one property in each item

I'm integrating Google Ads Rest API.I want to pass an array of type UserIdentifier to a function where each object should only have one item only because it is required by this Google Ads API for example:

f([{hashedEmail: "xxxxxx"}, {hashedPhoneNumber: "xxxxxx"}]) // OK
f([{hashedEmail: "xxxxxx", hashedPhoneNumber: "xxxxxx"}]) // Not Cool

This example comes close but I only want to use the keys that are mentioned in Google Ads API UserIdentifier type.

Thanks in advance.

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

0

It looks like the sort of structure you are looking for is a C-Style Union. This means that you have an object that only has one property present out of all of the listed ones. A helper generic type can easily be created to generate this structure:

type CUnion<T extends Record<string, unknown>> = { [K in keyof T]: { [_ in K]: T[K] } & { [K2 in Exclude<keyof T, K>]?: undefined } }[keyof T];

// { ssn: boolean; webauth?: undefined } | { webauth: string; ssn?: undefined }
type UserID = CUnion<{
  ssn: boolean;
  webauth: string;
}>;

const asdf: UserID = {
  ssn: true,
};

const asdf2: UserID = {
  webauth: "hey"
};

// @ts-expect-error This correctly fails.
const asdf3: UserID = {
  ssn: true,
  webauth: "hey"
}

We need to make the other properties explicitly undefined and optional because TypeScript does not error when you specify properties in other parts of a union when they should not actually be there. Anyways, here is your code adjusted to use this solution:

type CUnion<T extends Record<string, unknown>> = { [K in keyof T]: { [_ in K]: T[K] } & { [K2 in Exclude<keyof T, K>]?: undefined } }[keyof T];

type UserIdentifier = CUnion<{
  hashedEmail: string,
  hashedPhoneNumber: string,
  mobileId: string,
  thirdPartyUserId: string,
  addressInfo: {
    hashedFirstName: string,
    hashedLastName: string,
    city: string,
    state: string,
    countryCode: string,
    postalCode: string,
    hashedStreetAddress: string
  }
}>;

declare const f: (identifiers: UserIdentifier[]) => void;

f([{ hashedEmail: "xxxxxx" }, { hashedPhoneNumber: "xxxxxx" }]) // OK -- correctly works!
f([{ hashedEmail: "xxxxxx", hashedPhoneNumber: "xxxxxx" }]) // Not Cool -- correctly errors!

TypeScript Playground Link

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