首页 > 其他分享 >[Typescript] Function overload: Data hook problem

[Typescript] Function overload: Data hook problem

时间:2023-02-02 16:01:57浏览次数:46  
标签:Function Typescript overload initialData params fetchData getData data useData

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

相关文章

  • Higher Order Functions Continued (array methods, function methods)
    HigherOrderFunctionsGreeeaaaat. What'sahigherorderfunction,though? →A higherorderfunction isafunctionthatdoesatleastoneofthefollowing......
  • Command line arguments (argc, argv in main function)
    argc(ARGumentCount)isintandstoresnumberofcommand-lineargumentspassedbytheuserincludingthenameoftheprogram.Soifwepassavaluetoaprog......
  • Higher Order Functions
    AbstractionsAbstraction is:theprocessofhidingawaynecessary,butimmaterialdetails…toallowforaprogrammertoworkmorecloselywiththeproblem......
  • DISP_FUNCTION用法
    DISP_FUNCTION(theClass,pszName,pfnMember,vtRetVal,vtsParams)参数theClass 类名。pszName扩展函数名。pfnMember成员函数名。vtRetVal指定函数的返回类......
  • [Typescript] Generics in Function Overloads
    Here'safunctioncalled returnWhatIPassInExceptFor1:functionreturnWhatIPassInExceptFor1(t:unknown):unknown{if(t===1){return2;}return......
  • Functions (Closures, Optional Arguments)
    AQuickReviewonHoistingWhat'shoisting? →hoisting istheprocessingof declarations beforeanycodeisexecuted.what'sa declaration?a declarati......
  • Node.js+Koa2+TypeScript技术概览
    最近几年一直使用Node.js作为后端服务平台,通过Koa2框架中间件快速搭建Web服务,但是使用JavaScript开发大型后端服务时会使程序变得难以维护,继而使用TypeScript语言开发,使编......
  • error C2039: "function": 不是 "std" 的成员
    这个错误通过某度没找都合适的解决方案,故记录下来其实如果使用google搜索错误的英文关键词,大概第一条就是解决问题的链接Largenumberof"'function'isnotamembe......
  • ftrace中的Max Stack Tracer和Function_Profiler
    (1)MaxStackTracer的使用这个tracer记录内核函数的堆栈使用情况,需要使能CONFIG_STACK_TRACER,用户可以使用如下命令打开该tracer:#echo1>/proc/sys/kernel/stack......
  • TypeScript基础的使用
    一、类型注释类型注释=>作用:给js添加类型约束,约束是什么类型,就需要是什么类型语法=>类型letnum:Number=18conststr:String='张三'console.log(num);con......