首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何输入自定义pilet api?

如何输入自定义pilet api?
EN

Stack Overflow用户
提问于 2019-12-25 16:30:03
回答 1查看 96关注 0票数 2

我们使用的是微前端框架piral。对于我们的pilets,我们想要提供一个自定义的api。按照教程的要求,我们编写了如下代码

代码语言:javascript
复制
export interface MyApi {
  example(): void;
}

export function createMyApi(): Extend<MyApi> {
  return context => {
    return (api, target) => {
      return {
        example() {
          alert(`Hello from ${target.name}!`);
        }
      };
    };
  };
}

虽然这似乎在功能上可以工作,但我们无法让它与typescript一起工作。我们做错了什么?我们如何在我们的pilets中提供类型呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-26 04:24:32

我认为您可能错过了正确的声明合并。

代码语言:javascript
复制
import { PiletApi } 'piral-core';

declare module 'piral-core/lib/types/custom' {
  interface PiletCustomApi extends MyApi {}
}

所以总而言之:

代码语言:javascript
复制
import { PiletApi } 'piral-core';

export interface MyApi {
  example(): void;
}

declare module 'piral-core/lib/types/custom' {
  interface PiletCustomApi extends MyApi {}
}

export function createMyApi(): Extend<MyApi> {
  return context => {
    return (api, target) => {
      return {
        example() {
          alert(`Hello from ${target.name}!`);
        }
      };
    };
  };
}

希望这能有所帮助!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59476560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档