首页 > 其他分享 >[Typescript] Tips: Use 'extends' keyword to narrow the value of a generic

[Typescript] Tips: Use 'extends' keyword to narrow the value of a generic

时间:2022-10-03 18:22:50浏览次数:74  
标签:Use Typescript obj keyword Obj const Tips

export const getDeepValue = <Obj, FirstKey extends keyof Obj, SecondKey extends keyof Obj[FirstKey]>(
  obj: Obj,
  firstKey: FirstKey,
  secondKey: SecondKey
): Obj[FirstKey][SecondKey] => {
  return {} as any
}

const obj = {
  foo: {
    a: true,
    b: 2
  },
  bar: {
    c: "cool",
    d: 2
  }
}

const result = getDeepValue(obj, "bar", "d")

 

标签:Use,Typescript,obj,keyword,Obj,const,Tips
From: https://www.cnblogs.com/Answer1215/p/16750944.html

相关文章