Skip to content

Commit 2af8ac2

Browse files
committed
chore: cleanup types of Entity
1 parent d4d2c18 commit 2af8ac2

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

src/components.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { type JSX, type ParentProps, createMemo, createRenderEffect, mergeProps } from "solid-js"
1+
import {
2+
type JSX,
3+
type ParentProps,
4+
createMemo,
5+
createRenderEffect,
6+
mergeProps,
7+
splitProps,
8+
} from "solid-js"
29
import { Object3D } from "three"
310
import { threeContext, useThree } from "./hooks.ts"
411
import { manageSceneGraph, useProps } from "./props.ts"
5-
import type { Instance, Props } from "./types.ts"
12+
import type { Constructor, Instance, Overwrite, Props } from "./types.ts"
613
import { type InstanceFromConstructor } from "./types.ts"
714
import { augment, autodispose, isConstructor, isInstance, withContext } from "./utils.ts"
815

@@ -58,9 +65,12 @@ export const Portal = (props: PortalProps) => {
5865
/* */
5966
/**********************************************************************************/
6067

61-
type EntityProps<T extends object | (new (...args: any[]) => any)> = Omit<Props<T>, "from"> & {
62-
from: T
63-
}
68+
type EntityProps<T extends object | Constructor<object>> = Overwrite<
69+
Props<T>,
70+
{
71+
from: T
72+
}
73+
>
6474
/**
6575
* Wraps a `ThreeElement` and allows it to be used as a JSX-component within a `solid-three` scene.
6676
*
@@ -70,13 +80,16 @@ type EntityProps<T extends object | (new (...args: any[]) => any)> = Omit<Props<
7080
* optional children, and a ref that provides access to the object instance.
7181
* @returns The Three.js object wrapped as a JSX element, allowing it to be used within Solid's component system.
7282
*/
73-
export function Entity<T extends object | (new (...args: any[]) => object)>(props: EntityProps<T>) {
83+
export function Entity<T extends object | Constructor<object>>(props: EntityProps<T>) {
84+
const [config, rest] = splitProps(props, ["from", "args"])
7485
const memo = createMemo(() => {
7586
return augment(
76-
isConstructor(props.from) ? autodispose(new props.from(...(props.args ?? []))) : props.from,
87+
isConstructor(config.from)
88+
? autodispose(new config.from(...(config.args ?? [])))
89+
: config.from,
7790
{ props },
7891
) as Instance<T>
7992
})
80-
useProps(memo, props)
93+
useProps(memo, rest)
8194
return memo as unknown as JSX.Element
8295
}

src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,8 @@ export type Constructor<T = any> = new (...args: any[]) => T
177177
export type InstanceFromConstructor<TConstructor extends object | unknown> =
178178
TConstructor extends Constructor<infer TObject> ? TObject : TConstructor
179179

180-
/** Omit function-properties from given type. */
181-
type OmitFunctionProperties<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
182180
/** Overwrites the properties in `T` with the properties from `O`. */
183-
export type Overwrite<T, O> = Omit<T, OmitFunctionProperties<O>> & O
181+
export type Overwrite<T, O> = Omit<T, keyof O> & O
184182

185183
/**
186184
* Extracts the parameters of all possible overloads of a given constructor.

0 commit comments

Comments
 (0)