Typescript 中的 Mixin 类不能被 Rhum 模拟

发布于 2025-01-18 06:48:29 字数 1498 浏览 3 评论 0原文

您好社区,我有此代码在最后一行中失败。一旦评论,就可以按预期进行。问题是在嘲笑Mixin类时。

有人有任何想法,建议或想法如何解决这个问题吗?

我正在运行此操作:

  • DENO 1.19.3
  • 打字稿4.5.2

谢谢。

import { Rhum } from "https://deno.land/x/[email protected]/mod.ts";
import { assertInstanceOf } from "https://deno.land/[email protected]/testing/asserts.ts";

export interface IGame {}

export class NullGame implements IGame {}

function WithGameProperty() {
    return <T extends new (...args: any[]) => any>(GameProperty: T) => {
        return class extends GameProperty {
            private game: IGame = new NullGame();

            public set Game(game: IGame) {
                this.game = game;
            }

            public get Game() {
                return this.game;
            }
        };
    };
}

const PlayersEngine = WithGameProperty()(
    class PlayersEngine {
        onNextGenerationReady(listener: () => void): void {
        // add listener to react on an event
        }

        prepareNextGeneration(): void {
        // do some stuff and trigger an event
        }
    },
);

const pe = new PlayersEngine();

pe.Game = new NullGame();

assertInstanceOf(pe.Game, NullGame);

Rhum.mock(NullGame).create();
Rhum.mock(PlayersEngine).create();

Hello community I have this code which fails on the last line. Once it is commented out is is passing through as expected. The problem is when mocking the mixin class.

Have somebody any thoughts, recommendation or ideas how to deal with this problem?

I'm running this by:

  • Deno 1.19.3
  • Typescript 4.5.2

Thank you.

import { Rhum } from "https://deno.land/x/[email protected]/mod.ts";
import { assertInstanceOf } from "https://deno.land/[email protected]/testing/asserts.ts";

export interface IGame {}

export class NullGame implements IGame {}

function WithGameProperty() {
    return <T extends new (...args: any[]) => any>(GameProperty: T) => {
        return class extends GameProperty {
            private game: IGame = new NullGame();

            public set Game(game: IGame) {
                this.game = game;
            }

            public get Game() {
                return this.game;
            }
        };
    };
}

const PlayersEngine = WithGameProperty()(
    class PlayersEngine {
        onNextGenerationReady(listener: () => void): void {
        // add listener to react on an event
        }

        prepareNextGeneration(): void {
        // do some stuff and trigger an event
        }
    },
);

const pe = new PlayersEngine();

pe.Game = new NullGame();

assertInstanceOf(pe.Game, NullGame);

Rhum.mock(NullGame).create();
Rhum.mock(PlayersEngine).create();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2025-01-25 06:48:29

作为参考,我是Rhum的维护者之一。

问题在于Rhum的内部代码,即获得属性的逻辑,无法与Getters和Setter一起使用。我已经浏览了这一点,并设法获得了工作修复程序,所以这根本不是您的代码。

例如:

let desc = Object.getOwnPropertyDescriptor(original, property);
if (desc === undefined) {
  // property is a getter or setter
  desc = Object.getOwnPropertyDescriptor(this.constructor_fn.prototype, property)
}
// We can now use `desc.value`

我将为此添加一个修复程序和一个测试用例,并今晚发布新的补丁版本,因此请注意发行版:)

for reference, i'm one of the maintainers of Rhum.

The issue lies with Rhum's internal code, our logic that gets properties, doesn't work with getters and setters. I've toyed around with this and managed to get a working fix, so it isn't your code at all.

For example:

let desc = Object.getOwnPropertyDescriptor(original, property);
if (desc === undefined) {
  // property is a getter or setter
  desc = Object.getOwnPropertyDescriptor(this.constructor_fn.prototype, property)
}
// We can now use `desc.value`

I'll be adding a fix for this and a test case, and will release a new patch version tonight, so keep an eye out on the releases :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文