Magento 2: Error during compile Incompatible argument type

Magento 2: Error during compile Incompatible argument type

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2: Error during compile Incompatible argument type

Magento 2: Resolving the “Incompatible Argument Type” Error during Compilation

As a senior developer working extensively with Magento 2 platforms, I often encounter various compilation errors that can hinder smooth development processes. One such common issue is the “Incompatible argument type” error that arises when running the setup:di:compile command. Let’s delve into understanding this problem and provide a step-by-step guide to resolve it effectively.

Identifying the Root Cause

The error message you encountered stems from an incompatibility between the expected data type of a dependency in your custom module and the actual data type provided during object creation. In your case, the Ves\Blog\Model\ResourceModel\Author\Grid\Collection class expects an instance of \Magento\Framework\DB\Adapter\AdapterInterface, but it is receiving an instance of \Magento\Store\Model\StoreManagerInterface.

This mismatch occurs because you are manually injecting a Store Manager object into your custom collection, which is not compatible with the required adapter interface.

Understanding the Dependency Injection Issue

In Magento 2’s dependency injection architecture, each class should receive its dependencies based on their interfaces. The framework automatically resolves these dependencies using the Xml etc/di.xml config files and the TypeHintingStrategy. When you manually inject an object that does not match the expected interface in a constructor, it bypasses the automatic resolution process and leads to this compatibility error.

Fixing the Incompatible Argument Type Error

To address this issue, we need to ensure that our custom collection receives the correct adapter instance from the framework. This involves two key steps:

Step 1: Correct the Constructor Signature

In your Collection.php file, update the constructor signature to match the expected one provided by Magento’s AbstractCollection. Remove the unnecessary Store Manager parameter and replace it with the appropriate adapter interface. Here is an example of the corrected signature:

protected function __construct(
    \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Framework\DB\Adapter\AdapterInterface $resource,
    $mainTable = null
) {

Step 2: Update the Constructor Body

In the constructor body, ensure that you are using $resource instead of $storeManager. Update your constructor code as follows:

public function __construct(
    \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Framework\DB\Adapter\AdapterInterface $resource,
    $mainTable = null
) {
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $resource->getConnection(),
        $resource
    );

Best Practices for Magento 2 Dependency Injection

To avoid such errors in the future, follow these best practices when developing custom modules:

  1. Always use interfaces as constructor parameters, allowing the framework to resolve dependencies automatically.
  2. Avoid manually injecting objects that are not explicitly defined in your di.xml configuration files.
  3. Ensure that your module’s di.xml file is properly configured with the correct namespaces and class mappings.

Conclusion

In this post, we have explored the Magento 2 “Incompatible Argument Type” error during compilation and provided a comprehensive solution to resolve it effectively. By understanding the dependency injection architecture of Magento 2 and following the recommended best practices, you can avoid such errors and maintain a smooth development experience.

If you would like to learn more about Magento 2 development techniques and stay updated with the latest trends in e-commerce platforms, I recommend visiting Laravel Company for insightful articles and expert insights.

Magento 2 Extensions Admin Configuration

Our network