Open liuyang-kevin opened 2 years ago
最近在弄hook,想解构类型,发现分析器被any类型阻断了,是不是改成unknown更为合理
type Response = { code?: number; data?: unknown; // 生成any情形下, 下面hook的类型分析,就会被阻断 msg?: string; }; /** 获取当前登录账号信息 GET /admin/login */ export async function LogInfo(options?: { [key: string]: any }) { return request<API.Response & { data?: API.AdminInfo }>('/admin/login', { method: 'GET', ...(options || {}), }); } export function usePSP2<R extends (() => Promise<Awaited<ReturnType<R>>>)>(req: R) { type PSPBody = Awaited<ReturnType<R>>; type DataOf<T> = T extends { data?: infer E } ? E : never; // 这里就无法分析到正确的类型 const [isLoading, setLoading] = useState(true); const [body, setBody] = useState<PSPBody>(); const [data, setData] = useState<DataOf<PSPBody>>(); req().then(_body => { setBody(_body) }); return { isLoading, body, data, }; } const { body, data } = usePSP2(wj.psp.LogInfo); // 生成any情形下,这里data就无法分析到正确的类型,unknown是可以的
来个pr吧
最近在弄hook,想解构类型,发现分析器被any类型阻断了,是不是改成unknown更为合理