在打字稿中覆盖/重新声明具有泛型的类

发布于 2025-01-18 04:36:42 字数 1059 浏览 4 评论 0原文

目前我正在开发一个使用三个 js 和 typescript 的项目。我发现出于分组目的 Group 类。不幸的是 类型定义 Group 不带有像这样的泛型

meshGroup = new Group<Mesh>() 

,其中 meshGroup.children 属性仅包含 网格

我尝试了这个

declare class MeshGroup extends Group {
  children: Mesh[]
  add(...object: Mesh[]): this
}

meshGroup = new MeshGroup() 

,但得到了这个控制台

_classes_MeshGroup__WEBPACK_IMPORTED_MODULE_16__.MeshGroup is not a constructor

错误查找是否有一种简单的方法可以在打字稿中实现如此简单的包装器,但没有成功。

我的另一个解决方案是实现一个类型化数组

meshGroup: Mesh[] = []

并使其与类保持同步以处理类型,但它添加了另一层处理同步的层。

由于我不是使用打字稿的专家,是否有一个快速的类型定义技巧来实现这个“包装器”定义?

currently i am working on a project using three js and typescript. I have found out that for grouping purposes the Group class is recommended. Unfortunately the type definitions of Group dont come with a generic like this

meshGroup = new Group<Mesh>() 

of where the meshGroup.children property only consists of Meshes

i tried this

declare class MeshGroup extends Group {
  children: Mesh[]
  add(...object: Mesh[]): this
}

meshGroup = new MeshGroup() 

but got this console error

_classes_MeshGroup__WEBPACK_IMPORTED_MODULE_16__.MeshGroup is not a constructor

I looked up if theres an easy way to implement such simple wrapper in typescript but with no success.

My other solution would be to implement an typed array of

meshGroup: Mesh[] = []

and keep it in sync with the class to handle the types but it adds another layer of handling the sync.

Since i am not a pro at using typescript, is there a quick type definition trick to implement this "wrapper"-Definition?

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

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

发布评论

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

评论(2

想念有你 2025-01-25 04:36:42

When you call group.children you get an array Object3D[]

const group: THREE.Group = new THREE.Group();
const children = group.children as THREE.Mesh[];

// Now we can access Mesh attributes, like
const mat = children[0].material;

When you call group.children you get an array Object3D[] as per the definition file. This is because Group could have other Group, Sprite, Line, Point, Mesh, etc. inside it, so it defaults to the generic Object3D. However, if you're sure it will only contain Meshes, you could simply nudge it with:

const group: THREE.Group = new THREE.Group();
const children = group.children as THREE.Mesh[];

// Now we can access Mesh attributes, like
const mat = children[0].material;
£冰雨忧蓝° 2025-01-25 04:36:42

编辑

它也有可能用类型更安全的类扩展Group类并调用父函数(装饰器模式):


import { Group } from 'three'
import { Object3D } from 'three/src/core/Object3D'

export default class TypedGroup<T extends Object3D = Object3D> extends Group {
  children: T[] = []

  add(...object: T[]) {
    return super.add(...object)
  }

  remove(...object: T[]): this {
    return super.remove(...object)
  }

  attach(object: T): this {
    return super.attach(object)
  }

  getObjectById(id: number): T | undefined {
    return super.getObjectById(id) as T | undefined
  }

  getObjectByName(name: string): T | undefined {
    return super.getObjectByName(name) as T | undefined
  }

  getObjectByProperty(name: string, value: string): T | undefined {
    return super.getObjectByProperty(name, value) as T | undefined
  }
}

我的初始/旧答案

感谢您的回答@Marquizzo

这绝对可以是一个解决方案。目前,我想出了另一种方法,通过 patch-package 覆盖包并编辑Group.d.ts 文件如下:

export class Group<T extends Object3D = Object3D> extends Object3D {
    constructor();
    type: 'Group';
    readonly isGroup: true;
    children: T[];
    add(...object: T[]): this;
    remove(...object: T[]): this;
    attach(object: T): this;
    getObjectById(id: number): T | undefined;
    getObjectByName(name: string): T | undefined;
    getObjectByProperty(name: string, value: string): T | undefined;
}

除了您的答案之外,我还获得了方法的类型安全性,这对我也很重要!

我不知道这是否最适合我未来的需求,但目前这解决了我的类型成瘾:D

EDIT

Its also kinda possible extending the Group class with a more type-safer class and calling the parent functions (decorator pattern):


import { Group } from 'three'
import { Object3D } from 'three/src/core/Object3D'

export default class TypedGroup<T extends Object3D = Object3D> extends Group {
  children: T[] = []

  add(...object: T[]) {
    return super.add(...object)
  }

  remove(...object: T[]): this {
    return super.remove(...object)
  }

  attach(object: T): this {
    return super.attach(object)
  }

  getObjectById(id: number): T | undefined {
    return super.getObjectById(id) as T | undefined
  }

  getObjectByName(name: string): T | undefined {
    return super.getObjectByName(name) as T | undefined
  }

  getObjectByProperty(name: string, value: string): T | undefined {
    return super.getObjectByProperty(name, value) as T | undefined
  }
}

my initial/old answer

Thanks for the answer @Marquizzo!

That defenitely could be a solution. Currently i came up with another way with overriding the package via patch-package and edit the Group.d.ts file like this:

export class Group<T extends Object3D = Object3D> extends Object3D {
    constructor();
    type: 'Group';
    readonly isGroup: true;
    children: T[];
    add(...object: T[]): this;
    remove(...object: T[]): this;
    attach(object: T): this;
    getObjectById(id: number): T | undefined;
    getObjectByName(name: string): T | undefined;
    getObjectByProperty(name: string, value: string): T | undefined;
}

In addition to your answer i also get type-safety on the methods which was important to me too!

I dont know if this fits the best for my needs in the future but currently this solves my type-addiction :D

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