[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;

    ....
复制代码

 

posted @   Zhentiw  阅读(351)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-02-28 [Hapi.js] Serving static files
2016-02-28 [Hapi.js] Using the response object
2016-02-28 [Hapi.js] Replying to Requests
2016-02-28 [Hapi.js] Route parameters
2016-02-28 [Hapi.js] Logging with good and good-console
点击右上角即可分享
微信分享提示