Narrow inferred type when function has explicit return

前提是你 提交于 2021-02-09 10:53:14

问题


Look at the following code:

type Shape = null | string | string[] | { [key: string]: string | string[] }

interface ICfg<InShape extends Shape, OutShape extends Shape> {
  shapes?: readonly [InShape, OutShape]
  handle?(x: NonNullable<this["shapes"]>[0]): NonNullable<this["shapes"]>[1]
}

function apply<InShape extends Shape, OutShape extends Shape>(cfg: () => ICfg<InShape, OutShape>) {
  return cfg()
}

const shape = {
  type: "abc",
  date: "qwe",
}

var x = apply(() => ({
  shapes: [shape, shape],
  handle: x => null as any,
}))

Type of variable x is ICfg<{ type: string; date: string; }, { type: string; date: string; }> and that's perfect.

But let's change function syntax a bit: from () => ({ ... }) to () => { return { ... } }:

var y = apply(() => {
  return {
    shapes: [shape, shape],
    handle: x => null as any,
  }
})

Now type of y is ICfg<Shape, Shape> and that's bad.

Actually I have a bit more difficulties as I really need variable shape to be inside of the function (in reality function apply has arguments and shape is based on them):

var z = apply(() => {
  const shape = {
    type: "abc",
    date: "qwe",
  }

  return {
    shapes: [shape, shape],
    handle: x => null as any,
  }
})

This code produces same ICfg<Shape, Shape> for variable z.

I need variable z to have type ICfg<{ type: string; date: string; }, { type: string; date: string; }> like x does. Is there any way to achieve that without explicit specifying generic parameters of apply?

Full code in the playground.

来源:https://stackoverflow.com/questions/63184318/narrow-inferred-type-when-function-has-explicit-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!