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

相关推荐
夜空孤狼啸3 天前
前端常用拖拽组件库(vue3、react、vue2)
前端·javascript·react.js·typescript·前端框架·vue3
sunshine_程序媛4 天前
vue3中的watch和watchEffect区别以及demo示例
前端·javascript·vue.js·vue3
meng半颗糖5 天前
vue3 双容器自动扩展布局 根据 内容的多少 动态定义宽度
前端·javascript·css·vue.js·elementui·vue3
3D虚拟工厂5 天前
3D虚拟工厂
3d·vue3·blender·数字孪生·three.js
霸王蟹9 天前
前端项目Excel数据导出同时出现中英文表头错乱情况解决方案。
笔记·学习·typescript·excel·vue3·react·vite
佚名猫13 天前
vue3+vite+pnpm项目 使用monaco-editor常见问题
前端·vue3·vite·monacoeditor
技术闲聊DD15 天前
Vue3学习(4)- computed的使用
vue3·computed
蓝胖子的多啦A梦16 天前
Vue3 (数组push数据报错) 解决Cannot read property ‘push‘ of null报错问题
前端·vue3·push·数组数据
有梦想的攻城狮18 天前
从0开始学vue:vue3和vue2的关系
前端·javascript·vue.js·vue3·vue2
全栈小521 天前
【前端】Vue3+elementui+ts,TypeScript Promise<string>转string错误解析,习惯性请出DeepSeek来解答
前端·elementui·typescript·vue3·同步异步