Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!