gpt4 book ai didi

javascript - typescript (类型 'undefined' 不可分配给类型)为什么我的数组有一个 |不明确的?

转载 作者:行者123 更新时间:2023-11-30 19:45:23 25 4
gpt4 key购买 nike

我有一个名为 combinedMarkets 的数组,它是 5 个或 3 个不同市场数组的组合。所有这些数组都有以下接口(interface) IMarketAsset[]:

export interface IMarketAsset {
exchange: string;
base: string;
quote: string;
price_quote: string;
timestamp: string;
}

这是 typescript 错误发生的地方:

const combinedMarkets = asset !== 'BTC' && asset !== 'ETH' ?
btcMarkets.concat(ethMarkets).concat(marketUSD).concat(marketUSDC).concat(marketUSDT) :
marketUSD.concat(marketUSDC).concat(marketUSDT);

const filteredMarkets = combinedMarkets.length > 0 ? filterByUSDbase(asset, combinedMarkets) : [];

Argument of type '({ price_quote: string; exchange: string; base: string; quote: string; timestamp: string; } | undefined)[]' is not assignable to parameter of type 'IMarketAsset[]'. Type '{ price_quote: string; exchange: string; base: string; quote: string; timestamp: string; } | undefined' is not assignable to type 'IMarketAsset'. Type 'undefined' is not assignable to type 'IMarketAsset'.ts(2345)

const combinedMarkets: ({
price_quote: string;
exchange: string;
base: string;
quote: string;
timestamp: string;
} | undefined)[]

enter image description here

为什么 combinedMarkets 是 IMarketAsset 类型对象或 undefined object 的数组?

完整的 combineExchangeData 函数

// Filter by BTC, ETH, USD, USDT or USDC prices
// If asset has BTC/ETH pairing, obtain exchange BTC/ETH price to calculate assets USD/USDT value
export const combineExchangeData =
(asset: string, { marketBTC, marketETH, marketUSD, marketUSDT, marketUSDC }: IGetMarketsRes) => {
const btcBasedExchanges = marketBTC.filter((market: IMarketAsset) => market.base === asset);
const ethBasedExchanges = marketETH.filter((market: IMarketAsset) => market.base === asset);
const btcUSDTprices = marketUSDT.filter((market: IMarketAsset) => market.base === 'BTC');
const btcUSDprices = marketUSD.filter((market: IMarketAsset) => market.base === 'BTC');
const ethUSDTprices = marketUSDT.filter((market: IMarketAsset) => market.base === 'ETH');
const ethUSDprices = marketUSD.filter((market: IMarketAsset) => market.base === 'ETH');

const btcPricedMarkets = filterByExchangeBase(btcBasedExchanges, btcUSDTprices, btcUSDprices);
const ethPricedMarkets = filterByExchangeBase(ethBasedExchanges, ethUSDTprices, ethUSDprices);

const btcMarkets = btcPricedMarkets.filter((market) => R.not(R.isNil(market)));
const ethMarkets = ethPricedMarkets.filter((market) => R.not(R.isNil(market)));

const combinedMarkets = asset !== 'BTC' && asset !== 'ETH' ?
btcMarkets.concat(ethMarkets).concat(marketUSD).concat(marketUSDC).concat(marketUSDT) :
marketUSD.concat(marketUSDC).concat(marketUSDT);

console.log('combinedMarkets', combinedMarkets);
const filteredMarkets = combinedMarkets.length > 0 ? filterByUSDbase(asset, combinedMarkets) : [];
console.log('filteredMarkets', filteredMarkets);

if (R.isEmpty(filteredMarkets)) return [];

return filteredMarkets.map((market: IMarketAsset) => {
if (market) {
return {
...market,
price_quote: formatPrice(market.price_quote)
}
}
});
};

实用函数

这是我在主函数中使用的另外 2 个实用函数。此外,我已将问题缩小到 btcMarketsethMarkets 数组。所以看看 filterByExchangeBase

import * as R from 'ramda'

import { USD_CURRENCIES } from '../shared/constants/api'
import { IMarketAsset } from '../shared/types'

const calculateBasePrice = (assetBtcPrice: string | number, btcPrice: string | number) =>
(Number(assetBtcPrice) * Number(btcPrice)).toString();

export const filterByExchangeBase =
(exchanges: IMarketAsset[], usdtExchanges: IMarketAsset[], usdExchanges: IMarketAsset[]) =>
exchanges.map((exchange) => {
let basePriced = usdtExchanges.filter((btcExchange) => btcExchange.exchange === exchange.exchange)[0];

if (!basePriced) {
basePriced = usdExchanges.filter((btcExchange) => btcExchange.exchange === exchange.exchange)[0];
}

if (basePriced) {
const { price_quote: assetBtcPrice } = exchange;
const { price_quote: btcPrice } = basePriced;

return {
...exchange,
price_quote: calculateBasePrice(assetBtcPrice, btcPrice)
}
}
});

export const filterByUSDbase = (asset: string, combinedMarkets: IMarketAsset[] | undefined) => {
if (!combinedMarkets) return [];
return R.not(R.any(R.equals(asset))(USD_CURRENCIES))
? combinedMarkets.filter((marketAsset: IMarketAsset) => {
if (marketAsset && marketAsset.base) {
return marketAsset.base === asset;
}
}) : [];
}

最佳答案

Why is combinedMarkets an array either and object of type IMarketAsset or undefined

因为任何 marketXXX 数组,都是 IMarketAsset | 的数组未定义(而不仅仅是 IMarketAsset

关于javascript - typescript (类型 'undefined' 不可分配给类型)为什么我的数组有一个 |不明确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com