Error during setup:di:compile - Incompatible argument type

Error during setup:di:compile - Incompatible argument type

July 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Error during setup:di:compile - Incompatible argument type

Incompatible Argument Type Errors in Magento 2 DI Compilation

You’re encountering Incompatible argument type errors when running setup:di:compile in your Magento 2 installation. Let’s dive into the issue and find a resolution.

Understanding the Error

The error message suggests that the actual argument types being passed to certain methods don’t match the required types specified in the constructor declarations. In other words, you’re injecting arrays where objects or interfaces are expected.

Required Types vs Actual Types

  • Required Type: The type of object that the method expects to receive as an argument.

    • For instance, \Magento\Catalog\Model\ProductTypes\ConfigInterface expects an interface implementing specific methods related to product types.
    • \Magento\Wishlist\Model\WishlistFactory expects a factory for creating Wishlist objects.
  • Actual Type: The type of object that is actually being passed as an argument in your code.

Analyzing the Code

Looking at the provided code snippets, we can see that:

  1. In the first method (for the Quote\Create\Items block), you’re not explicitly defining the types for $wishlistFactory, $giftMessageSave, and other injected parameters in the constructor declaration.

  2. In the second method (for the Adminhtml\Order\ProductTypes block), you’re also missing type declarations for certain injected objects.

The Root Cause

The issue likely stems from:

  • Incorrect DI Configuration: Magento’s dependency injection relies on interface-based configuration and automatic resolution of dependencies. If the types aren’t explicitly declared or if there are conflicts in the configuration, this error can occur.

  • Compatibility Issues with Extensions/Overrides: If you’ve overridden core classes or have third-party extensions installed that modify these objects, it could lead to type incompatibility.

Resolving the Problem

Solution 1: Explicitly Declare Types in Constructors

Update your constructor declarations to explicitly define the types of injected objects. Here’s an example:

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Model\Session\Quote $sessionQuote,
    \Magento\Sales\Model\AdminOrder\Create $orderCreate,
    PriceCurrencyInterface $priceCurrency,
    \Magento\Wishlist\Model\WishlistFactory $wishlistFactory, // Explicitly define the type
    \Magento\GiftMessage\Model\Save $giftMessageSave,
    \Magento\Tax\Model\Config $taxConfig,
    \Magento\Tax\Helper\Data $taxData,
    \Magento\GiftMessage\Helper\Message $messageHelper,
    StockRegistryInterface $stockRegistry, // Explicitly define the type
    StockStateInterface $stockState, // Explicitly define the type
    array $data = []
) {
    // Rest of your code remains unchanged
}

Solution 2: Check DI Configuration and Extensions

  • Ensure there are no conflicting or missing configurations in your di.xml files.
  • Verify that any overridden classes from third-party extensions maintain the required types.

Conclusion

Incompatible argument type errors typically occur due to mismatches between expected and actual types in object injection. By explicitly declaring types in constructor arguments and ensuring proper DI configuration, you can resolve these issues and successfully complete the setup:di:compile process.

For a more detailed guide on Magento 2 dependency injection and best practices, I recommend checking out our comprehensive blog post at Laravel Company’s Magento Development Blog.

Magento 2 Catalog Admin Configuration

Our network