【问题标题】:How to properly use types with flow and nodejs?如何正确使用带有 flow 和 nodejs 的类型?
【发布时间】:2019-03-12 08:18:15
【问题描述】:

如何使用 Flow 和 nodejs 制作以下用例?

要求:

  1. 类型定义在一个单独的文件中,现在让我们在Types.js中说全部

  2. 在另一个文件/模块中使用它:

    const MyRequiredType = require('Types').MyRequiredType;
    const methodWithInputTypeCheck = function(request: MyRequiredType){ }
    

尝试使用flow-aliases,但这似乎只有在同一文件中有声明时才有效。

谢谢。

【问题讨论】:

    标签: javascript node.js node-modules flowtype


    【解决方案1】:

    哦,最后好像简单多了。

    看起来我们可以使用标准。类:

    1./Types.js

    // @flow
    const Types =
    {
        SomethingWithUserRequest: class MyClass {
            userId: string;   //thanks to Babel
            userNick: string;
        }
    };
    
    module.exports = Types;
    

    2./SomeModule.js:

    const SomethingWithUserRequest = require('../service/Dto/Types').SomethingWithUserRequest;
    
    const TestFacade =
    {
        testFlow: function(
            request: SomethingWithUserRequest
        ) {
            console.log('userId', request.userId);
        }
    };
    
    module.exports = TestFacade;
    

    只有IDE还是糊涂,不这样提示。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2020-06-04
      • 2018-11-10
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多