[Flow] Declare types for application

本文介绍了如何使用InFlow进行全局类型声明,包括变量、函数及接口的声明方式,并展示了如何在React组件中应用这些全局类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

    ....

 

转载于:https://www.cnblogs.com/Answer1215/p/6479413.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值