如何使用打字稿在第三方课堂上定义方法? [英] How to define method on 3rd party class using typescript?

查看:40
本文介绍了如何使用打字稿在第三方课堂上定义方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展第三方课堂,但是很难让打字稿表现得很好.基本上,我不能在新方法中使用该类中已经定义的任何现有方法.

I'm trying to extend a 3rd party class but am having trouble getting typescript to play nice. Basically, I can't use any existing method already defined in the class in my new method.

一种解决方法是在 extensions.ts 中重新定义现有方法(见下文),但必须有更好的方法.

A workaround would be to redefine existing methods in extensions.ts (see below), but there just has to be a better way.

export as namespace thirdParty;

export Class SomeClass {
  // some methods here
}

我的 extensions.ts

import {thirdParty} from 'thirdParty'

declare module 'thirdParty' {
    namespace thirdParty {
        class SomeClass{
            newMethod(): this

            // works if I redfine the method here
            originalExistingMethod(): number
        }
    }
}

thirdParty.SomeClass.prototype.newMethod = function() {
    return this.originalExistingMethod() + 1
}

当调用现有的方法(例如上面的 this.originalExistingMethod())时,打字稿会抱怨:

When calling an existing method like this.originalExistingMethod() above, typescript complains:

TS2339:类型'SomeClass'不存在属性'originalExistingMethod'

是否有一种方法可以避免执行

Is there a way to avoid having to redefine existing methods when performing module augmentation?

推荐答案

初始答案

以下是使用Tensorflow库的示例.

extend.ts

extend.ts

import { AdadeltaOptimizer } from '@tensorflow/tfjs-core';

declare module '@tensorflow/tfjs-core' {
    interface AdadeltaOptimizer {
        newMethod(message: string): void;
    }
}

AdadeltaOptimizer.prototype.newMethod = function (message: string) {
    console.log('===============');
    console.log(message);
    console.log('===============');
}

index.ts

import { AdadeltaOptimizer } from '@tensorflow/tfjs';
import "./extend";

const optimizer = new AdadeltaOptimizer(10, 10);

// the existing method is present
const className = optimizer.getClassName();

// the augmentation is also present
optimizer.newMethod(`The className is ${className}.`);

在官方TypeScript文档中有一个类似的示例 ,其中通过 map 方法增强 Observable .

There is a similar example in the official TypeScript documentation, which augments Observable with a map method.

谢谢.虽然我的问题是在定义newMethod时使用现有方法.因此,在extend.ts中不在index.ts中.有什么想法吗?

Thanks. Though my issue is using existing methods when defining newMethod. So in extend.ts not in index.ts. Any ideas on this?

这也可以在 extend.ts 中使用,如下所示:

This also works in extend.ts as follows:

import { AdadeltaOptimizer } from '@tensorflow/tfjs-core';

declare module '@tensorflow/tfjs-core' {
    interface AdadeltaOptimizer {
        newMethod(message: string): void;
    }
}

AdadeltaOptimizer.prototype.newMethod = function (message: string) {

    // just access the original method on `this` 
    const className = this.getClassName();

    console.log('===============');
    console.log(className);
    console.log(message);
    console.log('===============');
}

这篇关于如何使用打字稿在第三方课堂上定义方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆