Skip to content

Commit 95c56ae

Browse files
authored
restore autocompletion for page objects in TypeScript (#5401)
* restore autocompletion for page objects in TypeScript * remove conditional logic for page object type generation * fix logic
1 parent ee40437 commit 95c56ae

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/command/definitions.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue)
229229
const importStrings = []
230230

231231
for (const name in pathsToType) {
232-
const relativePath = getPath(pathsToType[name], targetFolderPath, testsPath)
233-
// For ESM modules with default exports, we need to access the default export type
234-
if (relativePath.endsWith('.js')) {
235-
importStrings.push(`type ${name} = typeof import('${relativePath}')['default'];`)
232+
const originalPath = pathsToType[name]
233+
const relativePath = getPath(originalPath, targetFolderPath, testsPath)
234+
// For .js files with plain object exports, access .default to allow TypeScript to extract properties
235+
// For .ts files, the default export is handled differently by TypeScript
236+
if (originalPath.endsWith('.js')) {
237+
importStrings.push(`type ${name} = typeof import('${relativePath}').default;`)
236238
} else {
237239
importStrings.push(`type ${name} = typeof import('${relativePath}');`)
238240
}

test/runner/definitions_test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ describe('Definitions', function () {
107107
const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`)
108108
const extend = definitionFile.getFullText()
109109

110-
extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js')['default'];")
110+
// Page objects are exported as plain objects in .js files
111+
// Access .default to allow TypeScript to extract properties for autocompletion
112+
extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js').default;")
111113
assert(!err)
112114
done()
113115
})

0 commit comments

Comments
 (0)