diff --git a/lib/command/definitions.js b/lib/command/definitions.js index ade5c2535..95a371fa4 100644 --- a/lib/command/definitions.js +++ b/lib/command/definitions.js @@ -229,10 +229,12 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue) const importStrings = [] for (const name in pathsToType) { - const relativePath = getPath(pathsToType[name], targetFolderPath, testsPath) - // For ESM modules with default exports, we need to access the default export type - if (relativePath.endsWith('.js')) { - importStrings.push(`type ${name} = typeof import('${relativePath}')['default'];`) + const originalPath = pathsToType[name] + const relativePath = getPath(originalPath, targetFolderPath, testsPath) + // For .js files with plain object exports, access .default to allow TypeScript to extract properties + // For .ts files, the default export is handled differently by TypeScript + if (originalPath.endsWith('.js')) { + importStrings.push(`type ${name} = typeof import('${relativePath}').default;`) } else { importStrings.push(`type ${name} = typeof import('${relativePath}');`) } diff --git a/test/runner/definitions_test.js b/test/runner/definitions_test.js index ea10085ba..f5c26d364 100644 --- a/test/runner/definitions_test.js +++ b/test/runner/definitions_test.js @@ -107,7 +107,9 @@ describe('Definitions', function () { const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`) const extend = definitionFile.getFullText() - extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js')['default'];") + // Page objects are exported as plain objects in .js files + // Access .default to allow TypeScript to extract properties for autocompletion + extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js').default;") assert(!err) done() })