小编dps*_*dps的帖子

类型“{}”不可分配给类型“Record<Key, Value>”

我有以下代码:

const foo = <Key extends keyof any, Value>() {
  type Rec = Record<Key, Value>
  const a: Rec = {}
}
Run Code Online (Sandbox Code Playgroud)

在第三行打字稿给出了一个错误Type '{}' is not assignable to type 'Record<Key, Value>。为什么会发生这种情况?

typescript

32
推荐指数
1
解决办法
1万
查看次数

'evolve' 函数的类型定义

我正在尝试为evolveramda 中的函数编写一个简单的类型定义。(https://ramdajs.com/docs/#evolve)。官方定义无法正常工作。

type Transformation<State> = {
  [Key in keyof State]: (x: State[Key]) => any
}

declare function evolve
  <State extends {}, Evolver extends Partial<Transformation<State>>>(evolver: Evolver, state: State):
  {[Key in keyof State]: Evolver[Key] extends (...args: any[]) => {} ? ReturnType<Evolver[Key]> : State[Key]}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在通用函数中使用它:

const foo = <State extends {a: string, b: string}>(state: State) => {
  const test = evolve({
    a: x => x,
    b: x => x
  }, state)
}
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

Argument of type '{ a: (x: …
Run Code Online (Sandbox Code Playgroud)

typescript ramda.js

9
推荐指数
1
解决办法
183
查看次数

在 PancakeSwap 上获取币安智能链代币价格的 API

我有一个代币的地址,我需要用 BUSD 或 BNB 来获取它的价格。如果没有其他方法,使用付费API不是问题。此代币可能未列在热门列表中,因此最好直接从 PancakeSwap 以某种方式获取价格。

binance-smart-chain

7
推荐指数
1
解决办法
5275
查看次数

考虑时区更改通过 date-fns 添加一天

在我的项目中,我使用 date-fns 进行日期操作。需要在某个范围内迭代天数。为此,我使用以下代码:

  for (
    // from and to are always start of some day, from <= to
    let date = from;
    isBefore(date, to) || isEqual(date, to);
    date = addDays(date, 1)
  ) {
    // Some operations with date
  }
Run Code Online (Sandbox Code Playgroud)

我期望date总是某一天的开始,但如果时区发生变化(冬季时间 -> 夏季时间),日期会比预期少 1 小时。这是一个例子:

const from = new Date('2019-03-31T00:00:00.000Z')
const fromPlusDay = dateFns.addDays(from, 1)

// I'm getting "2019-03-31T23:00:00.000Z"
// instead of "2019-04-01T00:00:00.000Z"
fromPlusDay.toISOString()
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我的时区是+2,转入夏令时间后变成了+3

javascript date date-fns

5
推荐指数
1
解决办法
2万
查看次数