ts踩坑!vue3中ts文件用export导出公共方法的ts类型定义

当我们有一个ts文件,定义并export出该function,其中方法里边有定义的变量,方法、钩子函数等多种,并最终return出该变量、方法。

此时 ts规则校验会让我们返回该函数类型。如下

cpp 复制代码
export default function () {
  const chart = ref();
  const sidebarElm = ref<Element>();


  const chartResizeHandler = (): void => {
    if (chart.value) {
      chart.value.resize();
    }
  };


  const sidebarResizeHandler = (e: TransitionEvent): void => {
    if (e.propertyName === 'width') {
      chartResizeHandler();
    }
  };


  const initResizeEvent = (): void => {
    window.addEventListener('resize', chartResizeHandler, { passive: true });
  };


  const destroyResizeEvent = (): void => {
    window.removeEventListener('resize', chartResizeHandler);
  };


  const initSidebarResizeEvent = (): void => {
    sidebarElm.value = document.getElementsByClassName('sidebar-container')[0];
    if (sidebarElm.value) {
      sidebarElm.value.addEventListener(
        'transitionend',
        sidebarResizeHandler as EventListener,
        { passive: true }
      );
    }
  };


  const destroySidebarResizeEvent = (): void => {
    if (sidebarElm.value) {
      sidebarElm.value.removeEventListener(
        'transitionend',
        sidebarResizeHandler as EventListener
      );
    }
  };


  const mounted = (): void => {
    initResizeEvent();
    initSidebarResizeEvent();
  };


  const beforeDestroy = (): void => {
    destroyResizeEvent();
    destroySidebarResizeEvent();
  };


  const activated = (): void => {
    initResizeEvent();
    initSidebarResizeEvent();
  };


  const deactivated = (): void => {
    destroyResizeEvent();
    destroySidebarResizeEvent();
  };


  return {
    chart,
    mounted,
    beforeDestroy,
    activated,
    deactivated
  };
} 

那么如何定义该方法的ts类型呢?

在TypeScript中,你可以通过接口(Interface)或类型别名(Type Alias)来定义函数的类型。对于给出的函数,我会创建一个类型别名来定义返回的对象结构,并且为函数本身定义一个更具体的类型。

cpp 复制代码
import { Ref } from 'vue';

// 定义chart和sidebarElm的引用类型
type ChartRef = Ref<any> | undefined; // 这里假设chart是一个具有resize方法的对象,但具体类型取决于你的库
type SidebarElmRef = Ref<Element | null>;

// 定义事件处理函数的类型
type ChartResizeHandler = () => void;
type SidebarResizeHandler = (e: TransitionEvent) => void;

// 定义init和destroy函数类型
type InitResizeEventHandler = () => void;
type DestroyResizeEventHandler = () => void;
type InitSidebarResizeEventHandler = () => void;
type DestroySidebarResizeEventHandler = () => void;

// 定义生命周期钩子类型
type LifecycleHook = () => void;

// 定义返回对象的类型
interface ResizeHandlerReturn {
  chart: ChartRef;
  mounted: LifecycleHook;
  beforeDestroy: LifecycleHook;
  activated: LifecycleHook;
  deactivated: LifecycleHook;
}

// 定义你的函数类型
type ResizeHandlerFunction = () => ResizeHandlerReturn;

// 实现你的函数并为其添加类型注解
export default function useResizeHandler(): ResizeHandlerReturn {
  const chart: ChartRef = ref();
  const sidebarElm: SidebarElmRef = ref<Element | null>(null);

  // ... 省略之前的实现细节 ...

  // 返回带有类型注解的对象
  return {
    chart,
    mounted,
    beforeDestroy,
    activated,
    deactivated
  };
}

注意几点:

1.我使用了Ref类型来自Vue,

2.我为chart定义了一个类型ChartRef,假设它是一个具有resize方法的对象,但你可能需要根据你的实际情况来具体定义它。

3.我为sidebarElm定义了一个类型SidebarElmRef,它是Ref<Element | null>,因为ref的初始值可能是null。

4.我为所有的事件处理函数和生命周期钩子定义了类型别名,以便在函数内部使用它们。

5.我定义了一个ResizeHandlerReturn接口来描述我的函数应该返回的对象结构。

6.最后,我定义了一个ResizeHandlerFunction类型别名来描述你的函数本身。

7.在实现函数时,我添加了类型注解来帮助理解,确保你的函数和返回的对象符合你定义的类型。

相关推荐
RuoyiOffice1 天前
SpringBoot3+Vue3 接口访问日志实战:@ApiAccessLog 慢接口、参数脱敏与 OperateLog 如何分工
spring boot·vue3·traceid·spring boot 3·访问日志·apiaccesslog·操作日志
tryCbest2 天前
Vue3项目在Nginx中配置代理后端IP
nginx·vue3
glustful4 天前
企业内部审计项目管理 系统开源(springboot+vue3)
vue3·springboot
RuoyiOffice4 天前
SpringBoot3+Vue3 会议室预订系统:时段冲突三模式检测 + 会前定时提醒与 cancelByBiz
spring boot·vue3·flowable·消息中心·spring boot 3·会议室·时段冲突
燐妤5 天前
中老年智能健康管理项目
python·ai·pycharm·vue3·fastapi·项目开发·健康
gis开发之家6 天前
《Vue3 从入门到大神50篇》Vue3 源码详解(二十):生命周期钩子源码解析 —— onMounted / onUpdated 如何实现?
前端·javascript·前端框架·vue3·vue3源码
java1234_小锋14 天前
Vue3专题 - 响应式基础
vue·vue3·响应式
春波petal15 天前
Vue3防抖搜索:从Lodash到customRef全解析
vue.js·vue3·防抖搜索
濮水大叔16 天前
不必把 Vue3 写成“麻花”:从状态碎片到对象协作,重新理解 Zova 的前端心智模型
前端·typescript·vue3·ioc·tsx·zova
听风34717 天前
智扫通机器人智能客服系统
vue3·fastapi·智能客服·agent项目