猿问

当我尝试实现接口时,为什么 flowtype 会抱怨?

在定义 a 时libdef,我有:


declare class Resource implements ResourceOptions {

        app ? : Express | Router;

        sequelize ? : Sequelize;

        model: any;

        endpoints ? : string[];

        actions ? : Array < "create" | "read" | "update" | "delete" | "list" > ;

        include ? : any[];

        pagination ? : boolean;

        updateMethod ? : "POST" | "PUT" | "PATCH";

        search ? : SearchOptions | SearchOptions[];

        sort ? : SortOptions;

        reloadInstances ? : boolean;

        associations ? : boolean;

        excludeAttributes ? : string[];

        readOnlyAttributes ? : string[];

        associationOptions: any;

        attributes: any;

        projects: any;

        controllers: any;

        create: Controllers$create;

        read: Controllers$read;

        update: Controllers$update;

        delete: Controllers$update;

        list: Controllers$list;

        all: Controllers$all;

        associationsInfo: any;

        use: any;

    }

和:


declare interface ResourceOptions {

        app ? : Express | Router;

        sequelize ? : Sequelize;

        model: any;

        endpoints ? : string[];

        actions ? : Array < "create" | "read" | "update" | "delete" | "list" > ;

        include ? : any[];

        pagination ? : boolean;

        updateMethod ? : "POST" | "PUT" | "PATCH";

        search ? : SearchOptions | SearchOptions[];

        sort ? : SortOptions;

        reloadInstances ? : boolean;

        associations ? : boolean;

        excludeAttributes ? : string[];

        readOnlyAttributes ? : string[];

    }

但是 flow 一直在抱怨,特别是关于endpoints ?: string[];:


 • undefined [2] is incompatible with array type [3] in property endpoints.

我该怎么做才能让它发挥作用?


慕丝7291255
浏览 190回答 1
1回答

慕桂英4014372

我并不是真正了解类和接口如何在流中交互的专家,但基本上接口不允许属性存在但未定义的可能性。如果你联合void到属性类型,错误就会消失。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答