首页 > 其他分享 >[React Typescript] Fixing type inference in a Custom React Hook

[React Typescript] Fixing type inference in a Custom React Hook

时间:2023-08-16 19:55:59浏览次数:35  
标签:defaultId Typescript const React Expect inference setId type id

// Problem
import { useState } from "react";
import { Equal, Expect } from "../helpers/type-utils";

export const useId = (defaultId: string) => {
  const [id, setId] = useState(defaultId);

  return [id, setId];
};

const [id, setId] = useId("1");

type tests = [
  Expect<Equal<typeof id, string>>,
  Expect<Equal<typeof setId, React.Dispatch<React.SetStateAction<string>>>>
];

the return type is actually:

(string | React.Dispatch<React.SetStateAction<string>> | undefined)[]

 

Two way to solve the problem:

1. Force the return type for the hook: not great, but working

import { useState } from "react";
import { Equal, Expect } from "../helpers/type-utils";

export const useId = (
  defaultId: string,
): [string, React.Dispatch<React.SetStateAction<string>>] => {
  const [id, setId] = useState(defaultId);

  return [id, setId];
};

const [id, setId] = useId("1");

type tests = [
  Expect<Equal<typeof id, string>>,
  Expect<Equal<typeof setId, React.Dispatch<React.SetStateAction<string>>>>,
];

 

2. using as const

import { useState } from "react";
import { Equal, Expect } from "../helpers/type-utils";

export const useId = (defaultId: string) => {
  const [id, setId] = useState(defaultId);

  return [id, setId] as const;
};

const [id, setId] = useId("1");

type tests = [
  Expect<Equal<typeof id, string>>,
  Expect<Equal<typeof setId, React.Dispatch<React.SetStateAction<string>>>>,
];

 

标签:defaultId,Typescript,const,React,Expect,inference,setId,type,id
From: https://www.cnblogs.com/Answer1215/p/17636053.html

相关文章

  • typeScript学习-interface和type 区别
    typeScript学习interface(接口)和type区别type和接口类似,都用来定义类型,但type和interface区别如下:区别1:定义类型范围不同interface只能定义对象类型或接口当名字的函数类型。type可以定义任何类型,包括基础类型、联合类型、交叉类型,元组。//type定义基础类型typ......
  • react 状态管理
    sroreindex.tsximportReactfrom'react';//仓库文件桶importStoreContentfrom'./StoreContent';importSetDataBtnfrom'./SetDataBtn';importShowDataBoxfrom'./ShowDataBox';functionIndex(){return(<&......
  • React(涉及基础和Usestate)
    ReactReact是JavaScript的一个类库;RenderingUserInterfacesReactTounderstandhowReactworks,wefirstneedabasicunderstandingofhowbrowsersinterpretyourcodetocreateinteractiveuserinterfaces(UI).浏览器构建用户界面原理Whenauservisitsa......
  • React和Vue的区别,大家怎么看?
    Vue更适合小项目,React更适合大公司大项目;Vue的学习成本较低,很容易上手,但项目质量不能保证......真的是这样吗?借助本篇文章,我们来从一些方面的比较来客观的去看这个问题。 论文档的丰富性从两个方面来看这个问题:社区的支持力度及文档的完善性。 对于任何编程语......
  • 图解 history api 和 React Router 实现原理
    Router是开发React应用的必备功能,那ReactRouter是怎么实现的呢?今天我们就来读一下ReactRouter的源码吧!首先,我们来学一下HistoryAPI,这是基础。什么是history呢?就是这个东西:我打开了一个新的标签页、然后访问baidu.com、sougou.com、taobao.com。长按后退按钮,就会列出......
  • 前端周刊第66期:TypeScript教程、ESM、React泡沫、htmx、测试文章
    周刊同步发表于微信公众号“写代码的宝哥”,欢迎各位小伙伴前来关注......
  • vue3+typescript中的props
     以上是子组件 以上是父组件<scriptsetuplangs="ts">letprops=defineProps(['info','money'])//父子组件的通信需要用到defineProps方法去接受父组件想要传递的数据console.info(props)</script>需要注意的就是:props可以实现父子组件的通信,但是props的......
  • ❤ React08-React 组件的生命周期3
    ❤React08-React组件的生命周期33组件卸载时(卸载阶段)执行时间:组件从页面之中消失时componentDidMount(){}组件的挂载阶段componentWillUnmount(){}组件的卸载阶段组件的挂载阶段执行的一些方法可以在组件的卸载阶段除去组件的其他钩子函数旧版本生命周期钩子函数新版......
  • ❤ React07-React 组件的生命周期2
    函数周期render生命周期渲染componenetDidUpdate()生命周期更新注意部分:seState应该放在判断if里面ajax的请求部分应该放在这里组件的生命周期三卸载阶段......
  • [React Typescript] Generic Inference through Multiple Type Helpers
    import{Equal,Expect}from"../helpers/type-utils";interfaceButton<T>{value:T;label:string;}interfaceButtonGroupProps<T>{buttons:Button<T>[];onClick:(value:T)=>void;}constButtonGroup=<......