我只是从官方流程网站复制了一个示例:

// @flow

function doSomething<T>(param: T): T {
  // ...
  return param;
}

doSomething<number>(3);


并得到错误:


  未捕获的ReferenceError:数字未定义


尽管以下代码可以工作:

function abc(a: number) {
  console.log(a);
}


第一个片段有什么问题?

"flow-bin": "^0.92.1",
"flow-runtime": "^0.17.0",
"flow-typed": "^2.4.0"


我与运行应用程序
npm run dev

这是完整的package.json

{
  "name": "myapp",
  "productName": "ElectronReact",
  "version": "0.13.2",
  "description": "Electron application boilerplate based on React, React Router, Webpack, React Hot Loader for rapid application development",
  "scripts": {
    "build": "concurrently \"npm run build-main\" \"npm run build-renderer\"",
    "build-dll": "cross-env NODE_ENV=development node --trace-warnings -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.renderer.dev.dll.js --colors",
    "build-main": "cross-env NODE_ENV=production node --trace-warnings -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.main.prod.js --colors",
    "build-renderer": "cross-env NODE_ENV=production node --trace-warnings -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.renderer.prod.js --colors",
    "dev": "cross-env START_HOT=1 node -r babel-register ./internals/scripts/CheckPortInUse.js && cross-env START_HOT=1 npm run start-renderer-dev",
    "electron-rebuild": "electron-rebuild --parallel --force --types prod,dev,optional --module-dir app",
    "flow": "flow",
    "flow-typed": "rimraf flow-typed/npm && flow-typed install --overwrite || true",
    "lint": "cross-env NODE_ENV=development eslint --cache --format=node_modules/eslint-formatter-pretty .",
    "lint-fix": "npm run lint -- --fix",
    "lint-styles": "stylelint app/*.css app/components/*.css --syntax scss",
    "lint-styles-fix": "stylefmt -r app/*.css app/components/*.css",
    "package": "npm run build && build --publish never",
    "package-all": "npm run build && build -mwl",
    "package-linux": "npm run build && build --linux",
    "package-win": "npm run build && build --win --x64",
    "postinstall": "node -r babel-register internals/scripts/CheckNativeDep.js && npm run flow-typed && npm run build-dll && electron-builder install-app-deps && node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
    "prestart": "npm run build",
    "start": "cross-env NODE_ENV=production electron ./app/",
    "start-main-dev": "cross-env HOT=1 NODE_ENV=development electron -r babel-register ./app/main.dev",
    "start-renderer-dev": "cross-env NODE_ENV=development node --trace-warnings -r babel-register ./node_modules/webpack-dev-server/bin/webpack-dev-server --config webpack.config.renderer.dev.js",
    "test": "cross-env NODE_ENV=test BABEL_DISABLE_CACHE=1 node --trace-warnings -r babel-register ./internals/scripts/RunTests.js",
    "test-all": "npm run lint && npm run flow && npm run build && npm run test && npm run test-e2e",
    "test-e2e": "cross-env NODE_ENV=test BABEL_DISABLE_CACHE=1 node --trace-warnings -r babel-register ./internals/scripts/RunTests.js e2e",
    "test-watch": "npm test -- --watch"
  },
  "browserslist": "electron 1.6",
  "build": {
    "productName": "ElectronReact",
    "appId": "org.develar.ElectronReact",
    "files": [
      "dist/",
      "node_modules/",
      "app.html",
      "main.prod.js",
      "main.prod.js.map",
      "package.json"
    ],
    "dmg": {
      "contents": [
        {
          "x": 130,
          "y": 220
        },
        {
          "x": 410,
          "y": 220,
          "type": "link",
          "path": "/Applications"
        }
      ]
    },
    "win": {
      "target": [
        "nsis"
      ]
    },
    "linux": {
      "target": [
        "deb",
        "AppImage"
      ],
      "category": "Development"
    },
    "directories": {
      "buildResources": "resources",
      "output": "release"
    }
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/chentsulin/electron-react-boilerplate.git"
  },
  "author": {
    "name": "C. T. Lin",
    "email": "[email protected]",
    "url": "https://github.com/chentsulin"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/chentsulin/electron-react-boilerplate/issues"
  },
  "keywords": [
    "electron",
    "boilerplate",
    "react",
    "redux",
    "flow",
    "sass",
    "webpack",
    "hot",
    "reload"
  ],
  "homepage": "https://github.com/chentsulin/electron-react-boilerplate#readme",
  "jest": {
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/internals/mocks/fileMock.js",
      "\\.(css|less|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "js"
    ],
    "moduleDirectories": [
      "node_modules",
      "app/node_modules"
    ],
    "transform": {
      "^.+\\.js$": "babel-jest"
    },
    "setupFiles": [
      "./internals/scripts/CheckBuiltsExist.js"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.2.1",
    "babel-jest": "^22.1.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-dev-expression": "^0.2.1",
    "babel-plugin-flow-runtime": "^0.18.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-es2015-classes": "^6.24.1",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-react-hmre": "^1.1.1",
    "babel-preset-react-optimize": "^1.0.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "chalk": "^2.3.0",
    "concurrently": "^3.5.1",
    "cross-env": "^5.1.3",
    "cross-spawn": "^6.0.4",
    "css-loader": "^0.28.9",
    "detect-port": "^1.2.2",
    "electron": "^1.7.11",
    "electron-builder": "^19.55.3",
    "electron-devtools-installer": "^2.2.3",
    "electron-rebuild": "^1.7.3",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.1",
    "eslint": "^4.16.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-formatter-pretty": "^1.3.0",
    "eslint-import-resolver-webpack": "^0.8.4",
    "eslint-plugin-compat": "^2.2.0",
    "eslint-plugin-flowtype": "^3.2.1",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jest": "^21.7.0",
    "eslint-plugin-jsx-a11y": "6.0.3",
    "eslint-plugin-promise": "^3.6.0",
    "eslint-plugin-react": "^7.6.1",
    "express": "^4.16.2",
    "extract-text-webpack-plugin": "^3.0.2",
    "fbjs-scripts": "^0.8.1",
    "file-loader": "^1.1.6",
    "flow-bin": "^0.92.1",
    "flow-runtime": "^0.17.0",
    "flow-typed": "^2.4.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^22.1.4",
    "minimist": "^1.2.0",
    "node-sass": "^4.7.2",
    "npm-logical-tree": "^1.2.1",
    "react-test-renderer": "^16.2.0",
    "redux-logger": "^3.0.6",
    "rimraf": "^2.6.2",
    "sass-loader": "^6.0.6",
    "sinon": "^4.2.2",
    "spectron": "^3.8.0",
    "style-loader": "^0.20.1",
    "stylefmt": "^6.0.0",
    "stylelint": "^8.4.0",
    "stylelint-config-standard": "^18.0.0",
    "uglifyjs-webpack-plugin": "1.1.8",
    "url-loader": "^0.6.2",
    "webpack": "^3.10.0",
    "webpack-bundle-analyzer": "^2.9.2",
    "webpack-dev-server": "^2.11.1",
    "webpack-merge": "^4.1.1"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "bootstrap": "^4.0.0",
    "devtron": "^1.4.0",
    "electron-debug": "^1.5.0",
    "font-awesome": "^4.7.0",
    "history": "^4.7.2",
    "immutable": "^3.8.2",
    "jquery": "^3.3.1",
    "normalizr": "^3.2.4",
    "popper.js": "^1.14.3",
    "react": "^16.7.0",
    "react-alert-template-basic": "^1.0.0",
    "react-dom": "^16.7.0",
    "react-hot-loader": "^4.0.0-beta.13",
    "react-notification-system": "^0.2.17",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^5.0.0-alpha.6",
    "react-tooltip": "^3.6.1",
    "react-transition-group": "^2.3.1",
    "reactstrap": "^7.1.0",
    "redux": "^3.7.2",
    "redux-form": "^7.3.0",
    "redux-form-input-masks": "^1.1.4",
    "redux-persist": "^5.10.0",
    "redux-saga": "^0.16.2",
    "source-map-support": "^0.5.3"
  },
  "devEngines": {
    "node": ">=7.x",
    "npm": ">=4.x",
    "yarn": ">=0.21.3"
  }
}


.flowconfig

[ignore]
<PROJECT_ROOT>/node_modules/*
<PROJECT_ROOT>/app/main.prod.js
<PROJECT_ROOT>/app/main.prod.js.map
<PROJECT_ROOT>/app/dist/.*
<PROJECT_ROOT>/resources/.*
<PROJECT_ROOT>/release/.*
<PROJECT_ROOT>/dll/.*
<PROJECT_ROOT>/release/.*
<PROJECT_ROOT>/git/.*

[include]

[libs]

[options]
esproposal.class_static_fields=enable
esproposal.class_instance_fields=enable
esproposal.export_star_as=enable
module.name_mapper.extension='css' -> '<PROJECT_ROOT>/internals/flow/CSSModule.js.flow'
module.name_mapper.extension='styl' -> '<PROJECT_ROOT>/internals/flow/CSSModule.js.flow'
module.name_mapper.extension='scss' -> '<PROJECT_ROOT>/internals/flow/CSSModule.js.flow'
module.name_mapper.extension='png' -> '<PROJECT_ROOT>/internals/flow/WebpackAsset.js.flow'
module.name_mapper.extension='jpg' -> '<PROJECT_ROOT>/internals/flow/WebpackAsset.js.flow'
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue

最佳答案

显式类型参数(如<number>)仅在Babel 7中可用。您的代码正在作为doSomething < number执行。

09-16 10:04