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

相关推荐
是梦终空11 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统
Irene19912 天前
通用消息组件 bug 修复及更好的实现是使用函数调用组件
vue3·函数调用·通用消息组件
我讲个笑话你可别哭啊2 天前
鸿蒙ArkTS快速入门
前端·ts·arkts·鸿蒙·方舟开发框架
Irene19913 天前
Vuex4:专为 Vue 3 设计,提供完整 TypeScript 支持
vue3·vuex4
无法长大3 天前
如何判断项目需不需要用、能不能用Tailwind CSS
前端·css·vue.js·elementui·vue3·tailwind css
cui_win4 天前
企业级中后台开源解决方案汇总
开源·vue3·ts
Sapphire~5 天前
Vue3-19 hooks 前端数据和方法的封装
前端·vue3
記億揺晃着的那天5 天前
Vue3 动态路由在生产环境才出现白屏的排查与解决(keep-alive 踩坑实录)
vue3·vue router·动态路由·生产环境报错
kong79069289 天前
Vue3快速入门
前端·vue3
无法长大10 天前
Mac M1 环境下使用 Rust Tauri 将 Vue3 项目打包成 APK 完整指南
android·前端·macos·rust·vue3·tauri·打包apk