diff --git a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
index 42cbb5713f5683c160bedbcfda7c299ca8d64d9e..76d6f1947bd945256027a0b8d780e740a8af18bb 100644
--- a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
+++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
@@ -444,7 +444,7 @@ protected function scanDirectory($dir, $include_tests) {
         $type = FALSE;
         $file = $fileinfo->openFile('r');
         while (!$type && !$file->eof()) {
-          preg_match('@^type:\s*(\'|")?(\w+)\1?\s*$@', $file->fgets(), $matches);
+          preg_match('@^type:\s*(\'|")?(\w+)\1?\s*(?:\#.*)?$@', $file->fgets(), $matches);
           if (isset($matches[2])) {
             $type = $matches[2];
           }
diff --git a/core/modules/system/tests/modules/module_info_type_comment/module_info_type_comment.info.yml b/core/modules/system/tests/modules/module_info_type_comment/module_info_type_comment.info.yml
new file mode 100644
index 0000000000000000000000000000000000000000..6216d4fde1232749be26157c4abe5d3ae716bf9b
--- /dev/null
+++ b/core/modules/system/tests/modules/module_info_type_comment/module_info_type_comment.info.yml
@@ -0,0 +1,5 @@
+name: 'Module info type comment test'
+type: module # Test that extension discovery works with a trailing comment for the type element.
+description: 'Support module for module system testing.'
+package: Testing
+version: VERSION
diff --git a/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php
index 7d6eaf054107a4f72deab68b2295841521aa6b65..70809aabbec2e449b9098d706beab37c7f8e750a 100644
--- a/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php
+++ b/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php
@@ -98,6 +98,17 @@ public function testExtensionDiscoveryCache() {
     ], $file_cache->get($this->root . '/core/modules/user/user.info.yml'));
   }
 
+  /**
+   * Tests finding modules that have a trailing comment on the type property.
+   *
+   * @covers ::scan
+   */
+  public function testExtensionDiscoveryTypeComment(): void {
+    $extension_discovery = new ExtensionDiscovery($this->root, TRUE, [], 'sites/default');
+    $modules = $extension_discovery->scan('module', TRUE);
+    $this->assertArrayHasKey('module_info_type_comment', $modules);
+  }
+
   /**
    * Adds example files to the filesystem structure.
    *