使用typescript的类型不存在属性

时间:2017-06-15 12:03:14

标签: typescript protractor

App.ts

import { $, WebElement } from 'protractor';
export class App {

  public appName: WebElement;

  constructor() {
    this.appName = $('#appName');
  }
}

app.e2e.test.ts

import { browser, $ } from 'protractor';
import { App } from '../pageobjects/App'
const BASE_URL = 'http://localhost:1344';

describe('App', () => {

  beforeEach(() => {
    browser.get(BASE_URL);
  });

  it('should load the Sites UI homepage', () => {
    console.log(App.appName);
  });
});

知道为什么我无法访问App.ts中定义的属性吗?

错误:

> tsc -p tsconfig.test.json && protractor dist/protractor.config.js

test/e2e/app.e2e.test.ts(15,28): error TS2339: Property 'appName' does not exist on type 'typeof App'.

1 个答案:

答案 0 :(得分:2)

在您的例外情况中,它非常清楚错误:

property 'appName' does not exist on type 'typeof App'.

要使其正常工作,您需要声明一个App的新实例,如下所示:

describe('App', () => {
  app: App;

  beforeEach(() => {
    browser.get(BASE_URL);
    app = new App();
  });

  it('should load the Sites UI homepage', () => {
    console.log(app.appName);
  });
});