flow.js静态类型检查

flow入门demo

  1. 初始化一个flow项目
mkdir flow-demo
cd flow-demo
npm init -y
npm i flow-bin flow-remove-types --save-dev //安装flow
  1. 修改package.json
"main": "src/index.js",
"scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "flow": "flow",
   "build": "flow-remove-types src -d dist"
},
"devDependencies": {
   "flow-bin": "^0.104.0",
   "flow-remove-types": "^2.104.0"
}
  1. 命令行执行npm run flow init创建.flowconfig文件
  2. 创建文件flow-demo/src/index.js如下
//@flow
var username : string = "lyt";
var password : number = "123456";
  1. 命令行执行npm run flow,结果报错如图
    在这里插入图片描述
    说明var password : number = "123456"; 处报错,声明number类型却赋值string类型,需要改为var password : number = 123456; 。修改之后命令行执行npm run flow 结果如图在这里插入图片描述
  2. 命令行执行npm run build ,打包后的文件在flow-demo/dist

webstrom集成flow

打开file->settings->Language & Framework->JavaScript界面。
将JavaScript Version选为Flow并选择Flow executable,如图
webstrom集成flow

vs code集成flow

安装vscode-flow-ide或者Flow-Language-Support插件。

flow原始类型

  • Boolean
  • String
  • Number
  • Null
  • Undefined
  • Symbol
// @flow
function method(x: number, y: string, z: boolean) {
  // ...
}
method(3.14, "hello", true); //works
"foo" + {};    // Error!
"foo" + [];    // Error!
  1. 多类型声明
//@flow
var username: string = "lyt";
var password: number = 123456;
var a: string | number = 3333;
function getColor(name: "success" | "warning" | "danger") {}
  1. 可能类型?type
    表示可能是type类型,也可能是nullundefined
// @flow
//函数参数是?type类型
function acceptsMaybeString(value: ?string) {
  // ...
}
acceptsMaybeString("bar");     // Works!
acceptsMaybeString(undefined); // Works!
acceptsMaybeString(null);      // Works!
acceptsMaybeString();          // Works!

//对象属性是?type类型
function acceptsObject(value: { foo?: string }) {
  // ...
}
acceptsObject({ foo: "bar" });     // Works!
acceptsObject({ foo: undefined }); // Works!
acceptsObject({ foo: null });      // Error!
acceptsObject({});  
  1. 函数参数默认值
// @flow
function acceptsOptionalString(value: string = "foo") {
  // ...
}

acceptsOptionalString("bar");     // Works!
acceptsOptionalString(undefined); // Works!
acceptsOptionalString(null);      // Error! 有默认值的可以传 undefined 但不能传 null
acceptsOptionalString();          // Works!
  1. 类声明

详细语法

类型详情文档查看flow官方文档

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个流行的 JavaScript 框架,用于构建用户界面。TypeScript 可以为 JavaScript 提供静态类检查和更好的代码提示功能。这两个工具结合使用可以帮助您创建精美的流程图。 以下是创建流程图的基本步骤: 1. 安装 Vue.js 和 TypeScript 在终端中使用以下命令安装 Vue.js 和 TypeScript: ``` npm install vue npm install typescript ``` 2. 创建一个 Vue.js 组件 使用 Vue.js 创建一个组件来包含流程图。这个组件将渲染流程图并处理用户的输入。 ```typescript <template> <div> <!-- 流程图渲染区域 --> </div> </template> <script lang="ts"> import { Component, Vue } from 'vue-property-decorator'; @Component({ name: 'Flowchart' }) export default class Flowchart extends Vue { // 组件逻辑 } </script> <style lang="scss"></style> ``` 3. 定义流程图节点和连接 定义节点和连接的类,并创建一个节点列表和连接列表。这些列表应该在组件的 data 属性中定义。 ```typescript interface Node { id: number; name: string; x: number; y: number; } interface Link { source: number; target: number; } @Component({ name: 'Flowchart' }) export default class Flowchart extends Vue { nodes: Node[] = [ { id: 1, name: 'Start', x: 100, y: 100 }, { id: 2, name: 'Step 1', x: 200, y: 200 }, // ... ]; links: Link[] = [ { source: 1, target: 2 }, { source: 2, target: 3 }, // ... ]; } ``` 4. 渲染流程图 使用 SVG 渲染流程图。为每个节点创建一个圆圈,并连接它们以表示流程。 ```typescript <template> <div> <svg> <g v-for="node in nodes" :key="node.id"> <circle :cx="node.x" :cy="node.y" r="30"></circle> <text :x="node.x" :y="node.y">{{node.name}}</text> </g> <path v-for="link in links" :key="link.source + '-' + link.target" :d="linkPath(link)"></path> </svg> </div> </template> <script lang="ts"> import { Component, Vue } from 'vue-property-decorator'; interface Node { id: number; name: string; x: number; y: number; } interface Link { source: number; target: number; } @Component({ name: 'Flowchart' }) export default class Flowchart extends Vue { nodes: Node[] = [ { id: 1, name: 'Start', x: 100, y: 100 }, { id: 2, name: 'Step 1', x: 200, y: 200 }, // ... ]; links: Link[] = [ { source: 1, target: 2 }, { source: 2, target: 3 }, // ... ]; linkPath(link: Link): string { const source = this.nodes.find((node) => node.id === link.source); const target = this.nodes.find((node) => node.id === link.target); const dx = target.x - source.x; const dy = target.y - source.y; const dr = Math.sqrt(dx * dx + dy * dy); return `M${source.x},${source.y}A${dr},${dr} 0 0,1 ${target.x},${target.y}`; } } </script> <style lang="scss"></style> ``` 5. 添加交互 通过响应用户的鼠标点击和拖拽事件,允许用户交互式地编辑流程图。 ```typescript <template> <div> <svg> <g v-for="node in nodes" :key="node.id"> <circle :cx="node.x" :cy="node.y" r="30" :class="{ selected: selectedId === node.id }" @mousedown="startDrag(node, $event)" @mouseup="stopDrag" ></circle> <text :x="node.x" :y="node.y">{{node.name}}</text> </g> <path v-for="link in links" :key="link.source + '-' + link.target" :d="linkPath(link)"></path> </svg> </div> </template> <script lang="ts"> import { Component, Vue } from 'vue-property-decorator'; interface Node { id: number; name: string; x: number; y: number; } interface Link { source: number; target: number; } @Component({ name: 'Flowchart' }) export default class Flowchart extends Vue { nodes: Node[] = [ { id: 1, name: 'Start', x: 100, y: 100 }, { id: 2, name: 'Step 1', x: 200, y: 200 }, // ... ]; links: Link[] = [ { source: 1, target: 2 }, { source: 2, target: 3 }, // ... ]; selectedId: number | null = null; startDrag(node: Node, event: MouseEvent): void { this.selectedId = node.id; const element = event.target as SVGGraphicsElement; element.addEventListener('mousemove', this.drag); } stopDrag(): void { this.selectedId = null; document.removeEventListener('mousemove', this.drag); } drag(event: MouseEvent): void { if (this.selectedId) { const node = this.nodes.find((n) => n.id === this.selectedId); if (node) { node.x += event.movementX; node.y += event.movementY; } } } linkPath(link: Link): string { const source = this.nodes.find((node) => node.id === link.source); const target = this.nodes.find((node) => node.id === link.target); const dx = target.x - source.x; const dy = target.y - source.y; const dr = Math.sqrt(dx * dx + dy * dy); return `M${source.x},${source.y}A${dr},${dr} 0 0,1 ${target.x},${target.y}`; } } </script> <style lang="scss"> .selected { fill: pink; } </style> ``` 现在,您已经创建了一个基本的流程图。您可以自定义节点和连接的外观,并使用更高级的技术来添加更多功能,如撤销和重做,输入和输出等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值