Skip to content

Commit

Permalink
chore(types): add back ReactThreeFiber namespace, math types (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Dec 31, 2022
1 parent cec4297 commit 0df10bb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/fiber/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './core'
import * as ReactThreeFiber from './three-types'
export { ReactThreeFiber }
export * from './three-types'
export * from './core'
export * from './web/Canvas'
export { createPointerEvents as events } from './web/events'
2 changes: 2 additions & 0 deletions packages/fiber/src/native.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as ReactThreeFiber from './three-types'
export { ReactThreeFiber }
export * from './three-types'
export * from './core'
export * from './native/Canvas'
Expand Down
28 changes: 16 additions & 12 deletions packages/fiber/src/three-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ type NonFunctionKeys<P> = { [K in keyof P]-?: P[K] extends Function ? never : K
type Overwrite<P, O> = Omit<P, NonFunctionKeys<O>> & O

interface MathRepresentation {
set(...args: any[]): any
set(...args: number[]): any
}
interface VectorRepresentation extends MathRepresentation {
setScalar(s: number): any
}
type WithMathProps<P> = {
[K in keyof P]: P[K] extends infer M
? M extends THREE.Color
? ConstructorParameters<typeof THREE.Color> | THREE.ColorRepresentation
: M extends MathRepresentation
? M extends VectorRepresentation
? M | Parameters<M['set']> | Parameters<M['setScalar']>[0]
: M | Parameters<M['set']>
: M
: P[K]
}

export type MathType<T extends MathRepresentation> = T extends THREE.Color
? ConstructorParameters<typeof THREE.Color> | THREE.ColorRepresentation
: T extends VectorRepresentation | THREE.Layers
? T | Parameters<T['set']> | number
: T | Parameters<T['set']>

export type Vector2 = MathType<THREE.Vector2>
export type Vector3 = MathType<THREE.Vector3>
export type Vector4 = MathType<THREE.Vector4>
export type Color = MathType<THREE.Color>
export type Layers = MathType<THREE.Layers>
export type Quaternion = MathType<THREE.Quaternion>

type WithMathProps<P> = { [K in keyof P]: P[K] extends MathRepresentation ? MathType<P[K]> : P[K] }

interface RaycastableRepresentation {
raycast(raycaster: THREE.Raycaster, intersects: THREE.Intersection[]): void
Expand Down
8 changes: 8 additions & 0 deletions packages/fiber/tests/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Array [
"Canvas",
"CanvasProps",
"Catalogue",
"Color",
"ComputeFunction",
"ConditionalType",
"ConstructorRepresentation",
Expand All @@ -35,13 +36,17 @@ Array [
"Instance",
"InstanceProps",
"Intersection",
"Layers",
"LegacyAlways",
"Loader",
"LoaderProto",
"LoaderResult",
"LoaderReturnType",
"MathType",
"ObjectMap",
"Performance",
"Quaternion",
"ReactThreeFiber",
"ReconcilerRoot",
"RenderCallback",
"RenderProps",
Expand All @@ -58,6 +63,9 @@ Array [
"ThreeEvent",
"UpdateCallback",
"UpdateSubscription",
"Vector2",
"Vector3",
"Vector4",
"Viewport",
"XRManager",
"_roots",
Expand Down

0 comments on commit 0df10bb

Please sign in to comment.