Skip to content

Conversation

@marklundin
Copy link
Collaborator

@marklundin marklundin commented Mar 28, 2025

Type Improvements

This pull request includes various changes across multiple files to improve type safety, enhance code readability, and update component interfaces. The most important changes involve updating prop types, adding validation, and enhancing documentation.

Types Safety
The type system has been drastically improved and now correctly surfaces the underlying types from the engine. Rather than statically re-declaring engine types, they are dynamically expose from the underlying PlayCanvas engine which is much more flexible and ensures the types always match the engine, regardless of the version installed

image

Runtime validation
Component types are now validated at runtime, providing helpful feedback for invalid props and falling back to sensible defaults to prevent crashes.

Unknown props
image

Invalid props
image

This should drastically improve the developer experience for dynamic prop validation. Warnings are only generated in under NODE_ENV === 'development' .

Changes below

Type Safety and Prop Updates:

Code Refactoring:

  • packages/lib/src/components/Anim.tsx, Camera.tsx, Collision.tsx, GSplat.tsx, Light.tsx, Render.tsx, RigidBody.tsx, Sprite.tsx: Updated component prop interfaces to use PublicProps for better type inference and consistency. [1] [2] [3] [4] [5] [6] [7] [8]

Validation and Documentation:

Other Improvements:

These changes collectively enhance the robustness and maintainability of the codebase by leveraging TypeScript's type system and adding necessary validations and documentation.

- Updated MotionEntity to use tuple types for position, rotation, and scale.
- Refactored Application, Entity, and various component files to extend PublicProps for better type safety.
- Removed deprecated warnOnce utility and improved error handling in components.
- Cleaned up commented-out code and improved documentation for clarity.
- Enhanced color handling in useMaterial and useColors hooks to support CSS color formats.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 28, 2025

Open in StackBlitz

npm i https://pkg.pr.new/playcanvas/react/@playcanvas/react@87

commit: 2c4d6fe

@marklundin marklundin self-assigned this Mar 28, 2025
@marklundin marklundin requested a review from Copilot March 28, 2025 19:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors component props to improve type safety and code readability by leveraging tuple types, robust prop validation, and dynamic extraction of public properties from PlayCanvas classes.

  • Removed the legacy warn-once utility in favor of a consolidated validation module
  • Enhanced type definitions and prop validation across components and hooks
  • Updated color handling and documentation to ensure consistency with PlayCanvas

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/lib/src/utils/warn-once.ts Removed legacy warning utility in favor of the new validation module
packages/lib/src/utils/validation.ts Added validation utilities and schema-based prop validation functions
packages/lib/src/utils/types-utils.ts Introduced PublicProps type for filtering out internal members
packages/lib/src/utils/color.ts Enhanced color prop validation and introduced dynamic color property extraction
packages/lib/src/hooks/use-material.tsx Refactored to dynamically retrieve color properties with proper type checking
packages/lib/src/components/Anim.tsx Updated prop interface with a mistake in component type import
packages/lib/src/Entity.tsx Improved prop validation with a schema, with a default rotation inconsistency addressed
packages/lib/src/Application.tsx Refactored Application component to leverage updated type definitions
packages/docs/src/components/MotionEntity.tsx Updated prop types to tuples for improved type safety
Comments suppressed due to low confidence (1)

packages/lib/src/Entity.tsx:119

  • The default rotation value here is inconsistent with the schema, which expects a quaternion ([0, 0, 0, 1]). Update the default to [0, 0, 0, 1] to ensure correct behavior.
rotation = [0, 0, 0],

- Updated AnimProps to extend PublicProps for AnimComponent, enhancing type safety.
- Removed commented-out validateAndSanitize function from validation.ts for cleaner code.
- Updated JSDoc comments for Anim, Camera, Collision, GSplat, Light, Render, Script, Sprite, useApp, useMaterial, and useParent components to include parameter descriptions, return types, and usage examples.
- Improved clarity and consistency in documentation to aid developers in understanding component usage.
@marklundin marklundin added documentation Improvements or additions to documentation and removed chore labels Apr 3, 2025
- Updated validation utilities to use a new `createComponentDefinition` function, enhancing type safety and clarity in component prop validation.
- Refactored multiple components (Application, Entity, Anim, Camera, Collision, GSplat, Light, Render, RigidBody, Script, Sprite) to utilize the new validation structure.
- Improved documentation in components to include parameter descriptions and usage examples.
- Enhanced error handling and warnings for invalid props, guiding developers towards correct usage.
…or improved mock application handling

- Downgraded React and React-DOM to version 18.3.1 in package-lock.json for compatibility.
- Refactored multiple components (Application, Anim, Camera, Collision, GSplat, Light, Render, RigidBody, Sprite) to utilize a new `getNullApplication` and `getStaticNullApplication` for mock application instances.
- Enhanced validation and component definitions to improve type safety and error handling.
- Updated color utility functions to support CSS color formats and improved validation logic.
@marklundin marklundin requested a review from Copilot April 7, 2025 13:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

packages/lib/src/hooks/use-script.tsx:74

  • The arrow function 'toLowerCamelCase' is defined after its usage in the 'useScript' hook. Since arrow functions are not hoisted, consider moving its declaration above the hook to ensure it is available when used.
const toLowerCamelCase = (str: string) : string => str[0].toLowerCase() + str.substring(1);

@marklundin marklundin merged commit fa251a0 into main Apr 7, 2025
6 checks passed
@marklundin marklundin deleted the feat-dx-improvement branch April 7, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve developer experience with better documentation and type safety

2 participants