I'm just starting out with Typescript and I'm curious if there's a way to achieve the following:
I need to define properties that will be sent to Hubspot via two different endpoints. Both APIs require data fields labeled property
and name
, each with their own set of values. For example:
{
property: 'email',
value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9beffee8efdbfef6faf2f7b5f8f4f6">[email protected]</a>'
}
Or:
{
name: 'email',
value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6d2c3d5d2e6c3cbc7cfca88c5c9cb">[email protected]</a>'
}
Furthermore, I want to ensure that only specific fields are being sent. For instance:
export interface HubspotFields {
newsletter: boolean;
name: string;
email: string;
}
Is there a way to create a structure like this?:
export interface Test {
[either "property" or "name"]: [keyof HubspotFields (value can only be one of the keys from HubspotFields)];
value: [the type of the corresponding dynamic keyof HubspotFields (string
boolean)];
}
In simpler terms:
//This fails
{
something: 'email',
value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e296879196a2878f838b8ecc818d8f">[email protected]</a>'
}
//This fails
{
property: 'something',
value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="572332242317323a363e3b7934383a">[email protected]</a>'
}
//This fails
{
property: 'email',
value: 2312
}
//This passes
{
property: 'newsletter',
value: true
}
//This passes
{
name: 'email',
value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e094859394a0858d81898cce838f8f">[email protected]</a>'
}
I hope my goal is clear. If not, feel free to ask for more details. Thank you!