Skip to content

Commit b56667c

Browse files
committed
fix(metadata): support TS prefix operators in typeParser
1 parent 9c5f11f commit b56667c

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/generators/metadata/utils/__tests__/transformers.test.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ describe('transformTypeToReferenceLink', () => {
9393
);
9494
});
9595

96+
it('should correctly extract TS prefix operators and link the underlying type', () => {
97+
strictEqual(
98+
transformTypeToReferenceLink('typeof Compiler', {
99+
Compiler: '/api/Compiler',
100+
}),
101+
'typeof [`<Compiler>`](/api/Compiler)'
102+
);
103+
});
104+
105+
it('should not incorrectly strip prefixes if they are part of the type name (word boundary)', () => {
106+
strictEqual(
107+
transformTypeToReferenceLink('typeofSomething', {
108+
typeofSomething: '/api/typeofSomething',
109+
}),
110+
'[`<typeofSomething>`](/api/typeofSomething)'
111+
);
112+
});
113+
96114
it('should handle extreme nested combinations of functions, arrays, generics, unions, and intersections', () => {
97115
const input =
98116
'(str: string[]) => Promise<Map<string, number & string>, Map<string | number>>';

src/generators/metadata/utils/typeParser.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ export const parseType = (typeString, transformType) => {
206206
return parts.map(p => resolveOr(p, transformType)).join(joiner);
207207
}
208208

209+
const PREFIXES = ['typeof ', 'keyof ', 'readonly ', 'unique '];
210+
for (const prefix of PREFIXES) {
211+
if (trimmed.startsWith(prefix)) {
212+
const rest = trimmed.slice(prefix.length).trim();
213+
return `${prefix.trim()} ${resolveOr(rest, transformType)}`;
214+
}
215+
}
216+
209217
// Strip a trailing `[]` for now; reapply on the way out.
210218
const isArray = trimmed.endsWith('[]');
211219
const core = isArray ? trimmed.slice(0, -2).trim() : trimmed;

0 commit comments

Comments
 (0)