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

相关推荐
凯小默21 小时前
13-实现首页的基础结构
vue3
我叫张小白。1 天前
Vue3 插槽:组件内容分发的灵活机制
前端·javascript·vue.js·前端框架·vue3
是席木木啊1 天前
Vue3 + Axios 适配多节点后端服务:最小侵入式解决方案
vue3·axios·前端开发·技术方案设计
宁波阿成1 天前
基于Jeecgboot3.9.0的vue3版本前后端分离的flowable流程管理平台
vue3·springboot3·flowable·jeecgboot
盛夏绽放2 天前
新手入门:实现聚焦式主题切换动效(Vue3 + Pinia + View Transitions)
前端·vue3·pinia·聚焦式主题切换
我叫张小白。2 天前
Vue3 组件通信:父子组件间的数据传递
前端·javascript·vue.js·前端框架·vue3
凯小默2 天前
11-定义接口返回类型值
vue3
前端_yu小白2 天前
websocket在vue项目和nginx中的代理配置
vue.js·websocket·nginx·vue3·服务端推送
凯小默2 天前
07-封装登录接口
vue3
小晗同学2 天前
创建第一个Nuxt v4.x 应用
vue·vue3·nuxt·prettier·nuxt 4.x