为什么可以在没有导入的情况下流参考类

  • 本文关键字:导入 情况下 参考 flowtype
  • 更新时间 :
  • 英文 :


我不明白为什么会发生这种情况:

source/server.js:2
2: import type { Server } from "http"
               ^^^^^^ Named import from module `http`. This module has no named export called `Server`.

这是模块声明:

declare module "http" {
  declare class Server extends net$Server {
  ...

更新

好吧,现在很清楚Server是一种值,而不是类型,因此不能像类型一样导入,并且在流Javascript类中可以在类型级别使用。但是我仍然不明白一件事,为什么Server能够被引用为不被导入的类型?

❯ yarn run flow
yarn run v0.24.6
$ "/Users/jason.kuhrt/projects/ssense/ms-fastly-purge/node_modules/.bin/flow"
No errors!
✨  Done in 0.18s.

cat ./source/test-flow.js

// @flow
const a: Server = 1
console.log(a)

cat ./.flowconfig

[ignore]
<PROJECT_ROOT>/build
<PROJECT_ROOT>/flow-typed
[include]
[libs]
[options]

cat ./package.json

{
  "name": "ms-fastly-purge",
  "version": "0.0.0",
  "scripts": {
    "postversion": "git push && git push --tags && release",
    "install-typings": "flow-typed install",
    "dev": "nodemon source/Main.js --exec babel-node",
    "lint": "eslint ./source",
    "test": "jest",
    "test:dev": "jest --watch",
    "build": "babel source/ --out-dir build/",
    "start": "node ./build/Main.js"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-eslint": "^7.2.3",
    "babel-preset-env": "^1.5.1",
    "babel-preset-flow": "^6.23.0",
    "babel-preset-stage-3": "^6.24.1",
    "eslint": "^3.19.0",
    "eslint-config-airbnb-base": "^11.2.0",
    "eslint-config-ssense": "^0.1.0",
    "eslint-plugin-flowtype": "^2.34.0",
    "eslint-plugin-fp": "^2.3.0",
    "eslint-plugin-import": "^2.3.0",
    "eslint-plugin-vue": "^2.0.1",
    "flow-bin": "^0.47.0",
    "flow-typed": "^2.1.2",
    "jest": "^20.0.4",
    "moxios": "^0.4.0",
    "nodemon": "^1.11.0",
    "prettier": "^1.3.1",
    "prettier-eslint": "^6.2.3",
    "release": "^1.2.2"
  },
  "dependencies": {
    "@ssense/node-logger": "^1.1.2",
    "axios": "^0.16.1",
    "base62": "^1.2.0",
    "bluebird": "^3.5.0",
    "body-parser": "^1.17.2",
    "convict": "^3.0.0",
    "express": "^4.15.3",
    "joi": "^10.5.2",
    "most": "^1.4.0",
    "newrelic": "^1.39.1",
    "ramda": "^0.24.0"
  },
  "babel": {
    "plugins": [],
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "flow",
      "stage-3"
    ]
  }
}

如果我更改为应该失败的事情,例如Server1,它确实失败了:

❯ yarn flow
yarn flow v0.24.6
$ "/Users/jason.kuhrt/projects/ssense/ms-fastly-purge/node_modules/.bin/flow"
source/test-flow.js:2
  2: const a: Server1 = 1
              ^^^^^^^ identifier `Server1`. Could not resolve name

服务器不是类型。这是一个类,因此您无法将其导入类型。如果您删除类型,则所有内容都应按预期工作。

最新更新