'How to express an argument can take one of two values in TypeScript?

Why asignment is not accepted?

partnerAndDeliveryData2![key1] = {};

First parameter of the method key1 should be either "partnerData" or "deliveryData". But seems method signator implementation does not limits it, but intialize an array?!

import React, { useEffect, useState, Fragment } from "react";

export type SetPartnerAndDeliveryDataIn = {
  partnerData?: NameAndAddress;
  deliveryData?: NameAndAddress;
  businessUseApproval?: boolean;
  email?: string;
  error?: string;
};

export type NameAndAddress = {
  name?: string;
  phoneNumber?: string;
  country?: string;
  postalCode?: string;
  city?: string;
  address?: string;
  taxCode?: string;
};

export default function PartnerAndDeliveryForm(props: {}) {

const [partnerAndDeliveryData, setPartnerAndDeliveryData] =
    useState<SetPartnerAndDeliveryDataIn>({
      partnerData: {
        country: "Magyarország",
      },
    });

const setData = (
    key1: ["partnerData", "deliveryData"],
    value: string
  ) => {
    let partnerAndDeliveryData2 = {
      ...partnerAndDeliveryData,
    };
    partnerAndDeliveryData2![key1] = {};
  }
} 

minimal reproducible example



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source