提问人:Brandon-Perry 提问时间:6/16/2023 更新时间:6/16/2023 访问量:28
我可以让 TypeScript 根据外部函数缩小范围吗?
Can I get TypeScript to narrow based on outer function?
问:
我想要一个函数来检查 X 或 Y 是否为 null,如果不是,则让 TypeScript 成功缩小类型范围。这可能吗?
const X: string | null
const Y: Record<string, Bar> | null
const checkNull = <T,>(cb:()=>T):T|null => !X || !Y ? null : cb()
const getBar = () => checkNull(()=> Y[X]) << currently says Y and X could be null
答: 暂无答案
评论
Bar