首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >键入命名函数后找不到名称

键入命名函数后找不到名称
EN

Stack Overflow用户
提问于 2020-12-04 22:24:55
回答 1查看 49关注 0票数 1

为命名函数定义接口后,ts不再看到实际的函数吗?

代码语言:javascript
运行
复制
interface InitiatePlayers {
    (this: App, arg0: { amount: string }): void;
}

<InitiatePlayers>function initiatePlayers({ amount }) {
    const PlayerWidth = this.width / 45;
    this.nodes.push(
        PlayerFactory({ width: PlayerWidth }),
        PlayerFactory({ width: PlayerWidth, id: 'player-2', x: this.width - PlayerWidth })
    );
};

app.onInit = function () {
    // Add Players to the board
    initiatePlayers.call(this, { amount: 2 }); // cannot find name of function
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-04 22:36:56

看起来像是<InitiatePlayers>语法把它搞乱了。

following code会正确检测函数的类型:

代码语言:javascript
运行
复制
const app:any = {};

interface InitiatePlayers {
    (this: any, arg0: { amount: string }): void;
}

const initiatePlayers: InitiatePlayers = function ({ 
  amount,
}) {

};

app.onInit = function () {
  initiatePlayers.call(this, { amount: 2 });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65145312

复制
相关文章

相似问题

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