gpt4 book ai didi

javascript - 导出接口(interface)出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:10 25 4
gpt4 key购买 nike

如果我导入一个接口(interface),流程无法识别它,但如果接口(interface)在同一个文件中声明,它会按预期工作。

工作示例:

interface Action {
execute(): Promise < any > ;
}

class CreateJuridicalPerson {
constructor() {}



static create() {
return new CreateJuridicalPerson();
}

}

(new CreateJuridicalPerson: Action);

//[flow] [flow] property `execute` of Action (Property not found in CreateJuridicalPerson)

失败的例子:

Action.js:

interface Action {
execute(): Promise < any > ;
}
export default Action;

CreateJurdicalPerson.js:

import Action from './Action'

class CreateJuridicalPerson {
constructor() {}



static create() {
return new CreateJuridicalPerson();
}

}

(new CreateJuridicalPerson: Action);

//quiet...

最佳答案

您的代码无效并引发以下错误

src/Action.js:7
7: export default Action;
^^^^^^ Action. type referenced from value position
3: interface Action {
^ type Action

如果您使用的是 Nuclide 并且没有看到错误,请尝试从命令行运行 flow check。问题是类型和接口(interface)必须使用 export typeexport interface 导出,然后使用 import type

导入

Action .js

// @flow

export interface Action {
execute(): Promise<any>;
}

索引.js

// @flow

import type { Action } from './Action'

class CreateJuridicalPerson {
constructor() {}
static create() {
return new CreateJuridicalPerson()
}
}

(new CreateJuridicalPerson: Action)

src/index.js:12
12: (new CreateJuridicalPerson: Action)
^^^^^^ property `execute` of Action. Property not found in
12: (new CreateJuridicalPerson: Action)
^^^^^^^^^^^^^^^^^^^^^^^^^ CreateJuridicalPerson

关于javascript - 导出接口(interface)出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41062209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com