cycold / cycold.github.io

Please dot not star...
4 stars 1 forks source link

flow a type-checking for js #128

Closed cycold closed 6 years ago

cycold commented 7 years ago

flow的模块申明

A declared module would be looked up by the typechecker only when a file implementing that module was not found by the resolution algorithm of the module system, or such a file was found but not checked. 

申明的模块只有在模块系统(npm)没有找到或者找到了模块但是没有添加`/* @flow */`标志的模块时, flow就会使用申明的模块,而不是实际找到的模块或者根本就不存在的模块.

这是flow处理内部没有遵循type-checking的模块方式,就是将其抽象出来单独进行申明, don't care it's implmentations

flow对import,export在type上的处理

在flow模块中,也可以使用export type ,import type指定要导出/导入某些模块中定义的type,而不是定义好的变量.

// imort 可以导入模块中使用type声明的类型 

// user.js
export type UserID = number;
export type User = {
  id: UserID,
  name: string,
};
export const age = 5;

export class VNode {
    el?: Object
}

import type {UserID,  User, VNode } from "user.js"
// 但是不能导出变量: import type { age } from "user.js" 
// 注意导出类型时, 需要加上type.

上面的类型既可以作为type导出使用,同时也可以作为变量导出使用
import {vnode} from "user.js"   // 作为变量

impot type {vnode} from "user.js"  //作为type导出

注意import type可以在模块中作为type导出的只有: 模块中定义的

  1. type
  2. interface
  3. class