0

I'm trying to type an object with nested objects, but keep getting an error.

Object literal may only specify known properties, and '"0x63564c40"' does not exist in type 'Network'.

export interface Network {
  network: {
    chainId: string;
    rpcUrls: string[];
    chainName: string;
    nativeCurrency: {
      name: string;
      symbol: string;
      decimals: number;
    };
    blockExplorerUrls: string[];
    iconUrls: string[];
  };
}

export const networkParams: Network = {
  "0x63564c40": {
    chainId: "0x63564c40",
    rpcUrls: ["https://api.harmony.one"],
    chainName: "Harmony Mainnet",
    nativeCurrency: { name: "ONE", decimals: 18, symbol: "ONE" },
    blockExplorerUrls: ["https://explorer.harmony.one"],
    iconUrls: ["https://harmonynews.one/wp-content/uploads/2019/11/slfdjs.png"],
  },
  "0xa4ec": {
    chainId: "0xa4ec",
    rpcUrls: ["https://forno.celo.org"],
    chainName: "Celo Mainnet",
    nativeCurrency: { name: "CELO", decimals: 18, symbol: "CELO" },
    blockExplorerUrl: ["https://explorer.celo.org"],
    iconUrls: ["https://celo.org/images/marketplace-icons/icon-celo-CELO-color-f.svg"],
  },
};

Why does it say so with the first, but not the second object?

2
  • It points out one error at a time. Try removing the first object and it will tell you that '"0xa4ec"' does not exist in type 'Network' Commented Aug 26, 2022 at 13:28
  • networkParams is supposed to implement Network, and Network does not have a key named 0x63564c40. Commented Aug 26, 2022 at 13:28

1 Answer 1

2

You named the key network if you want it to be dynamic you can do that like this:

export interface Network {
  [key: string]: {
    chainId: string;
    rpcUrls: string[];
    chainName: string;
    nativeCurrency: {
      name: string;
      symbol: string;
      decimals: number;
    };
    blockExplorerUrls: string[];
    iconUrls: string[];
  };
}

export const networkParams: Network = {
  "0x63564c40": {
    chainId: "0x63564c40",
    rpcUrls: ["https://api.harmony.one"],
    chainName: "Harmony Mainnet",
    nativeCurrency: { name: "ONE", decimals: 18, symbol: "ONE" },
    blockExplorerUrls: ["https://explorer.harmony.one"],
    iconUrls: ["https://harmonynews.one/wp-content/uploads/2019/11/slfdjs.png"],
  },
  "0xa4ec": {
    chainId: "0xa4ec",
    rpcUrls: ["https://forno.celo.org"],
    chainName: "Celo Mainnet",
    nativeCurrency: { name: "CELO", decimals: 18, symbol: "CELO" },
    blockExplorerUrls: ["https://explorer.celo.org"],
    iconUrls: ["https://celo.org/images/marketplace-icons/icon-celo-CELO-color-f.svg"],
  },
};
Sign up to request clarification or add additional context in comments.

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.