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;
    
        ....
  • 相关阅读:
    vue 多层级嵌套组件传值 provide 和 inject
    vue 消息订阅与发布 实现任意组件间的通信
    成功
    疯掉的拼接
    解析发送
    一条条发
    com发送
    字符串拼接
    COM
    笨方法的combox级联
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6479413.html
Copyright © 2011-2022 走看看