Skip to content

Commit 7dea111

Browse files
authored
Merge pull request #258 from tidev/fix/promise-generics
2 parents 9a0e863 + 2afeb59 commit 7dea111

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

packages/titanium-docgen/generators/typescript_generator.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ class DocsParser {
175175
// skip bundled documentation for modules and Node.js shims
176176
return;
177177
}
178-
const namespaceParts = typeInfo.name.split('.');
178+
// Handle generic types by extracting the base type name before the first '<'
179+
const typeName = typeInfo.name.split('<')[0];
180+
const namespaceParts = typeName.split('.');
179181
namespaceParts.pop();
180182
if (skipApis.includes(typeInfo.name)) {
181183
return;
@@ -622,7 +624,17 @@ class GlobalTemplateWriter {
622624

623625
if (Array.isArray(docType)) {
624626
const normalizedTypes = docType.map(typeName => this.normalizeType(typeName));
625-
return normalizedTypes.includes('any') ? 'any' : normalizedTypes.join(' | ');
627+
if (normalizedTypes.includes('any')) {
628+
return 'any';
629+
}
630+
// Parenthesize function types in unions to avoid TypeScript syntax errors
631+
const parenthesizedTypes = normalizedTypes.map(type => {
632+
if (type.includes(') => ')) {
633+
return `(${type})`;
634+
}
635+
return type;
636+
});
637+
return parenthesizedTypes.join(' | ');
626638
}
627639

628640
const lessThanIndex = docType.indexOf('<');
@@ -649,6 +661,9 @@ class GlobalTemplateWriter {
649661
}
650662
} else if (baseType === 'Dictionary') {
651663
return `Dictionary<${subType}>`;
664+
} else if (baseType === 'Promise') {
665+
// Use standard Promise<T> generic syntax
666+
return `Promise<${subTypes.join(', ')}>`;
652667
}
653668
}
654669

packages/titanium-docgen/validate.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,12 @@ function validateDataType(type, fullTypeContext) {
434434
// Should it have been written as a complex type?
435435
if (common.COMPLEX_TYPES.has(type)) {
436436
const argCount = common.COMPLEX_TYPES.get(type); // may be 0 if Function/Callback
437-
// Enforce as ERROR if Promise/Set/Map doesn't have exact generic type count
438-
const severity = [ 'Map', 'Set', 'Promise' ].includes(type) ? ERROR : WARNING;
437+
// Skip validation for Promise to allow Promise without generics (Promise<T> not supported by build system)
438+
if (type === 'Promise') {
439+
return errors;
440+
}
441+
// Enforce as ERROR if Set/Map doesn't have exact generic type count
442+
const severity = [ 'Map', 'Set' ].includes(type) ? ERROR : WARNING;
439443
errors.push(new Problem(`${type} ${severity === ERROR ? 'must' : 'should'} have ${argCount || 'any number of'} generic type(s) specified, but had 0: ${fullTypeContext || type}`, severity));
440444
} else if (type === 'Object') {
441445
// Warn about generic Object types (Dictionary is handled above as a complex type)

0 commit comments

Comments
 (0)