Requirement is if pass in initialData
, then return type should not contain undefined
, otherwise, it should.
import { it } from "vitest";
import { Equal, Expect } from "../helpers/type-utils";
// You'll need to use function overloads to figure this out!
function useData<T>(params: { fetchData: () => Promise<T>; }): {
getData: () => T | undefined;
};
function useData<T>(params: { fetchData: () => Promise<T>; initialData: T }): {
getData: () => T;
};
function useData<T>(params: { fetchData: () => Promise<T>; initialData?: T }): {
getData: () => T | undefined;
} {
let data = params.initialData;
params.fetchData().then((d) => {
data = d;
});
return {
getData: () => data,
};
}
it("Should return undefined if no initial data is passed", () => {
const numData = useData({
fetchData: () => Promise.resolve(1),
});
const data = numData.getData();
type Test1 = Expect<Equal<typeof data, number | undefined>>;
});
it("Should NOT return undefined if initial data is passed", () => {
const numData = useData({
fetchData: () => Promise.resolve(1),
initialData: 2,
});
const data = numData.getData();
type Test1 = Expect<Equal<typeof data, number>>;
});
标签:Function,Typescript,overload,initialData,params,fetchData,getData,data,useData From: https://www.cnblogs.com/Answer1215/p/17086297.html