Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/markdown-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ export const safelySeparateTypeStringOn = (typeString: string, targetChar: strin
switch (char) {
case '<':
case '{':
case '(':
depth++;
break;
case '>':
case '}':
case ')':
depth--;
break;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/markdown-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,25 @@ def fn():
).toMatchSnapshot();
});

it('should keep a conditional type with a parenthesized union as a single inner type', () => {
expect(
rawTypeToTypeInformation(
"Promise<string extends T ? (A | B) : T extends 'a' ? A : B>",
'',
null,
),
).toEqual({
collection: false,
type: 'Promise',
innerTypes: [
{
collection: false,
type: "string extends T ? (A | B) : T extends 'a' ? A : B",
},
],
});
});

it('should allow commas in object types', () => {
expect(
rawTypeToTypeInformation('Function<{a: string, b: string}>', '', null),
Expand Down Expand Up @@ -1125,6 +1144,16 @@ Second level methods.`;
const result = safelySeparateTypeStringOn(' string | number ', '|');
expect(result).toEqual(['string', 'number']);
});

it('should not split inside parentheses', () => {
const result = safelySeparateTypeStringOn('(A | B)[] | C', '|');
expect(result).toEqual(['(A | B)[]', 'C']);
});

it('should not split commas inside parentheses', () => {
const result = safelySeparateTypeStringOn('Function<(A, B)>, C', ',');
expect(result).toEqual(['Function<(A, B)>', 'C']);
});
});

describe('getTopLevelMultiTypes', () => {
Expand All @@ -1142,6 +1171,16 @@ Second level methods.`;
const result = getTopLevelMultiTypes('string');
expect(result).toEqual(['string']);
});

it('should not split a union wrapped in parentheses inside a conditional type', () => {
const result = getTopLevelMultiTypes('string extends T ? (A | B) : C');
expect(result).toEqual(['string extends T ? (A | B) : C']);
});

it('should still split top-level unions next to parenthesized types', () => {
const result = getTopLevelMultiTypes('(A | B) | C');
expect(result).toEqual(['(A | B)', 'C']);
});
});

describe('getTopLevelOrderedTypes', () => {
Expand Down
Loading