是否可以输入安全的 Strapi 生命周期钩子?

问题描述 投票:0回答:1

我正在尝试向 Strapi 生命周期添加类型,到目前为止,我在文档或谷歌搜索中没有发现任何有用的信息。

// foo/lifecycle.js

export default {
  afterCreate: (event: any) => {
    // event.result to be typed
  },
};

是否可以从 @strapi/strapi 中的某个隐藏位置获取事件类型? 顺便说一句,我正在使用 v4

typescript strapi
1个回答
0
投票

我设法像这样添加类型安全:

import { GetValues } from '@strapi/types/dist/types/core/attributes';

type LifecycleEvent<T> = Event & {
  result?: T;
}

// usage
afterCreate: (event: LifecycleEvent<GetValues<'admin::permission'>>) => {
  event.result.id // properly infers types
© www.soinside.com 2019 - 2024. All rights reserved.