zoukankan      html  css  js  c++  java
  • [Flow] Declare types for application

    In Flow, you can make global declarion about types.

    Run:

    flow init

    It will generate .flowconfig file, open it and add few lines of configration.

    [libs]
    decls/
    
    [ignore]
    .*/node_modules/.*

    So it says that go to find 'decls' folders and use what has been defined as global type checking.

    Declear a variable:

    declare type PetAction = 'adopt' | 'foster';

    Declear a function:

    declare type PetShelterDispatch = (x: PetShelterActions) => void;

    Declear an interface:

    declare type Pet = {
        name: string;
        id: number;
        from: string;
        type: PetType;
        locationId: number;
        action?: PetAction;
    };

    All those will be global available for React components.

    So you can use those, for example:

    // @flow
    module.exports = ([
        {
            type: 'dog',
            name: 'Snoopy',
            from: 'Charlie',
            locationId: 0,
            id: 0
        },
        {
            type: 'cat',
            name: 'Garfield',
            from: 'John',
            locationId: 0,
            id: 1
        }
    ]: Array<Pet>);

    It is also good to declear type for "state", 'props':

    // @flow
    
    const React = require('react');
    
    type ModalProps = {
        dispatch: PetShelterDispatch;
        pet: Pet;
    };
    type ModalState = {
        inquiry: ?PetInquiry;
    };
    
    class PetModal extends React.Component {
    
        props: ModalProps;
        state: ModalState;
        onSubmitClick: () => void;
    
        ....
  • 相关阅读:
    linux通过源码安装nodejs
    设置npm的镜像源
    ubuntu手动安装PhantomJS
    h2数据库的简单使用
    xampp启动失败 Apache shutdown unexpectedly
    phpqrcode实现二维码(含图片)
    php 使用 rabbitmq
    rabbitMQ linux安装
    rabbitmq的web管理界面无法使用guest用户登录
    linux安装使用xdebug
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6479413.html
Copyright © 2011-2022 走看看