接口:嵌套的对象类型



我试图键入一个带有嵌套对象的对象,但一直收到一个错误。

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"],
},
};

为什么它对第一个物体这么说,而对第二个物体却不这么说?

如果你想让密钥网络是动态的,你可以这样命名:

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"],
},
};

最新更新