0

I want to have data that would be as follow

paymentProviderByCountry = {
    England: worldPay,
    Spain: worldPay
}

How do I create the interface for paymentProviderByCountryObj that integrates paymentProvidersList and countryList in my desired dynamic format.

I guess what I am looking for is how do I create recursively the obj

    enum paymentProvidersList {
    WorldPay,
    Paypal,
}
enum countryListEnum {
    England,
    France,
    Spain,
}

type T0 = { [key in paymentProvidersList]: string }; // This would work

/*
  I want the values on paymentProviderByCountryObj to be dynamic based on the
   enum values so that i can have an object that looks like this

  paymentProviderByCountry = {
    England: WorldPay,
    Spain: Paypal,
    France: WorldPay
}
*/
interface paymentProviderByCountryObj {
    // This is the complex case, i cant get the index of the class to be dynamic and also its value
    [key in countryListEnum] :string : { [key in paymentProvidersList]: string }; 
}

export interface sysConfigInterface {
    paymentProviders: paymentProvidersList,
    paymentProviderByCountry: paymentProviderByCountryObj,
    minPaymentThreshold: String,
}

The Example Link

2 Answers 2

1

You're looking for in (introduced in TypeScript 2.1 as part of Mapped Types)

{ [key in countryList]: paymentProvidersList }

Or, in case you don't want to have to pass every country:

{ [key in countryList]?: paymentProvidersList }
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for that. can you show me a workable example that would join the results aka paymentProviderByCountry : { countryList : paymentProvidersList, countryList : paymentProvidersList}
Literally put that directly after the interface Whatever and you're good to go.
"TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'." so i cant just put it there as they are enum
Apparently, it's only valid in literal types, and not directly under interfaces. See typescriptlang.org/play/…. I learned something new today :D
Thank you for that but that assumes that the T2 knows key is set, whereas i want that value to be dynamic check the link at the end of my question as is too long to post here
|
0

This is what ultimately worked for my end goal

enum paymentProvidersList {
        WorldPay,
        Paypal,
    }
    enum countryListEnum {
        England,
        France,
        Spain,
    }

    // Or Record<countryListEnum, paymentProvidersList>
    type paymentProviderByCountryObj = { [k in countryListEnum]: paymentProvidersList } 

    const paymentProviderByCountry: paymentProviderByCountryObj = {
        [countryListEnum.England]: paymentProvidersList.WorldPay,
        [countryListEnum.France]: paymentProvidersList.WorldPay,
        [countryListEnum.Spain]: paymentProvidersList.Paypal
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.