sparkengineweb
    Preparing search index...

    Function Optional

    • Decorator to register optional property types for runtime type inference. Enables type inference directly from class instances.

      For type aliases, pass the resolved runtime class, not the alias itself. The ESLint rule optional-decorator-type-consistency enforces this automatically.

      Type Parameters

      • T

      Parameters

      • type: T

        The runtime class/constructor that represents the property's type. For type aliases, use the base runtime class they resolve to.

      Returns PropertyDecorator

      // For direct class types:
      class MaterialComponent {
      @Optional(Rgb)
      diffuseColor?: Rgb;
      }

      // For type aliases, use the base runtime class:
      // type CollisionCallback = SerializableCallback<...>
      class BoundingBoxComponent {
      @Optional(SerializableCallback) // Use runtime class, not alias
      onCollision?: CollisionCallback; // Type annotation can use alias
      }

      const material = new MaterialComponent();
      getOptionalType(material, 'diffuseColor') // Returns Rgb
      getOptionalType(material, 'onCollision') // Returns SerializableCallback