typescript 错误 运行 navalia 示例

typescript error running navalia example

我正在尝试 运行 来自 https://github.com/joelgriffith/navalia 的这个示例,但就我而言,我无法正常运行它:

navaliatest.ts

/// <reference path="typings.d.ts" />

import { Chrome } from 'navalia';
const chrome = new Chrome();

async function buyItOnAmazon() {
  const url = await chrome.goto('https://amazon.com');
  const typed = await chrome.type('input', 'Kindle');
  const clicked = await chrome.click('.nav-search-submit input');

  chrome.done();

  console.log(url, typed, clicked); // 'https://www.amazon.com/', true, true
}

buyItOnAmazon();

tsconfig.json

{
  "files": [
    "navaliatest.ts"
  ],
  "compilerOptions": {
    "noImplicitAny": false,
    "target": "es6",
    "moduleResolution": "node",
    "paths": {
      "*" : ["/usr/local/lib/node_modules/*"]
    }
  }
}

typings.d.ts

/// <reference path="/usr/local/lib/node_modules/navalia/build/Chrome.d.ts" />

declare module 'navalia' {
  var Chrome: any;
  export = Chrome;
}

以下是版本:

MacBook-Pro:testcasperjs myusername$ node --version
v6.11.2MacBook-Pro:testcasperjs myusername$ npm --version
3.10.10
MacBook-Pro:testcasperjs myusername$ tsc --version
Version 2.4.2

这是我得到的错误,尽管我得到了 .js 文件输出:

MacBook-Pro:testcasperjs myusername$ tsc navaliatest.ts
../../../usr/local/lib/node_modules/navalia/node_modules/chrome-launcher/chrome-finder.ts(203,16): error TS2339: Property 'from' does not exist on type 'ArrayConstructor'.
../../../usr/local/lib/node_modules/navalia/node_modules/chrome-launcher/chrome-launcher.ts(99,15): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
navaliatest.ts(3,10): error TS2305: Module ''navalia'' has no exported member 'Chrome'.

我确定某处存在愚蠢的错误,但请有人帮我看一下吗?谢谢。

您不需要重新声明 navalia。鉴于 moduleResolution 设置为 Node

,已在 node_modules/navalia/build/index.d.ts 为您完成

您需要将 module 设置为 commonjs 以便您可以 运行 它在节点

tsconfig.json

{
  "files": [
    "navaliatest.ts"
  ],
  "compilerOptions": {
    "noImplicitAny": false,
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "Node"
  }
}

navaliatest.ts(无变化)

import { Chrome } from 'navalia';
const chrome = new Chrome();

async function buyItOnAmazon() {
  const url = await chrome.goto('https://amazon.com');
  const typed = await chrome.type('input', 'Kindle');
  const clicked = await chrome.click('.nav-search-submit input');

  chrome.done();

  console.log(url, typed, clicked); // 'https://www.amazon.com/', true, true
}

buyItOnAmazon();

它将创建 navaliatest.js 并且没有错误,在节点中可以是 运行。