Как можно расширить (augmentation) класс из node_modules?

Как можно расширить (augmentation) класс из node_modules?

В этой статье описано как можно расширить обычные классы в проекте: https://www.digitalocean.com/community/tutorials/typescript-module-augmentation

Попробовал также расширить внешний класс - пишет TS2339: Property 'age' does not exist on type 'TransactionSettings'.

Не хочется ради одного метода вводить дополнительное имя и писать Class1 extends Ydb.Table.TransactionSettings

В этом пример ydb-sdk внешний модуль

пример:

import { Ydb } from 'ydb-sdk';

declare module 'ydb-sdk' {
    // eslint-disable-next-line @typescript-eslint/no-namespace
    namespace Table {
        interface TransactionSettings {
            age: number;
            walk(location: string): void;
        }
    }
}

// @ts-ignore
Ydb.Table.TransactionSettings.prototype.walk = (location: string) => `Likes to walk in the ${location}`;

const a = new Ydb.Table.TransactionSettings({ serializableReadWrite: {} });
console.log(a);
console.log(a.age);

Ответы (1 шт):

Автор решения: Gayrat Vlasov

Схема оказалась рабочей и в случае node_modules. В ydb-sdk вложенные namespace и добавление namespase Ydb помогло благодаря мудрому совету Grundy

import { Ydb } from 'ydb-sdk';

declare module 'ydb-sdk' {
    // eslint-disable-next-line @typescript-eslint/no-namespace
    namespace Ydb {
        // eslint-disable-next-line @typescript-eslint/no-namespace
        namespace Table {
            interface TransactionSettings {
                age: number;

                walk(location: string): void;
            }
        }
    }
}


Ydb.Table.TransactionSettings.prototype.walk = (location: string) => `Likes to walk in the ${location}`;
→ Ссылка