0

Есть вот такой код:

import Events from 'backbone-events-standalone';

class A {

 constructor(){
  Object.assign(this, Events);
 }

 doSomething(){
  this.trigger();
 }

}

Отнаследоваться не могу, потому что импортируется инстанс объекта.

Описания типов для этой библиотеки нет, но есть для Backbone.

declare module 'backbone-events-standalone' {

    import Backbone = require('backbone');

    export default Backbone.Events;

}

Однако компилятор TS ругается :

Error:(143, 9) TS2339: Property 'trigger' does not exist on type 'EventBus'.

Предлагали вот такие варианты решениния , но ошибка осталась, хотя webstorm уже пути находил.

1 ответ 1

0

Вот что пишут в документации про миксины:

To satisfy this requirement, we create stand-in properties and their types for the members that will come from our mixins. This satisfies the compiler that these members will be available at runtime. This lets us still get the benefit of the mixins, albeit with some bookkeeping overhead.

То есть, интерфейс миксина всё-таки придётся продублировать.

type IEvents = typeof Events;

class A implements IEvents {
    constructor() {
        Object.assign(this, Events);
    }

    trigger: (name: string) => this;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.