Home > Mobile >  typescript array of objects with one property in each item
typescript array of objects with one property in each item

Time:03-11

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.

CodePudding user response:

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

  • Related