diff --git a/utils/typescript.mjs b/utils/typescript.mjs index 258067d70b..ff2a55c519 100644 --- a/utils/typescript.mjs +++ b/utils/typescript.mjs @@ -504,9 +504,13 @@ function generateMethodDeclaration(method, options = {}) { .join(', '); let returnType = 'void'; - if (method.chainable && !globalFunction && options.currentClass !== 'p5') { - returnType = options.currentClass || 'this'; - // TODO: Decide what should be chainable. Many of these are accidental / not thought through + if (method.chainable && !globalFunction) { + // In global mode, use P5 (the imported class type) instead of p5 (the namespace) + if (options.inGlobalMode && options.currentClass === 'p5') { + returnType = 'P5'; + } else { + returnType = options.currentClass || 'this'; + } } else if (overload.return && overload.return.type) { returnType = convertTypeToTypeScript(overload.return.type, options); } else if (method.return && method.return.type) {