Ошибка импорта класса Typescript: в типе отсутствует свойство 'prototype' 'но требуется в типе' typeof ». TS (2741) - PullRequest
0 голосов
/ 13 апреля 2020

При попытке импортировать один компонент во второй компонент я получаю следующую ошибку.

Свойство 'prototype' отсутствует в типе 'InputComponent', но требуется в типе 'typeof InputComponent'. TS (2741)

test.ts

class InputComponent {

  add():number{
    return 1 + 1
  }
}

class Example {
  public input: typeof InputComponent;

  constructor(Input: typeof InputComponent) {
    this.input = new Input();
  }
}

class Example2 {
  public input: typeof InputComponent;

  constructor(Input: typeof InputComponent = InputComponent) {
    this.input = new Input();
  }
}

Вот снимок экрана с ошибкой и песочницей.

машинописная песочница

enter image description here

enter image description here

1 Ответ

0 голосов
/ 13 апреля 2020

Проблема в том, что вы ссылаетесь с public input на typeof InputComponent, но пытаетесь присвоить ему new InputComponent().

Изменение на public input: InputComponent решает проблему.

В машинописном тексте InputComponent относится к объекту класса, тогда как typeof InputComponent относится к самому классу.

class InputComponent {

  add():number{
    return 1 + 1
  }
}

class Example {
  public input: InputComponent;

  constructor(Input: typeof InputComponent) {
    this.input = new Input();
  }
}

class Example2 {
  public input: InputComponent;

  constructor(Input: typeof InputComponent = InputComponent) {
    this.input = new Input();
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...