Skip to content

Commit aeeb0ae

Browse files
author
Soc Sieng
committed
Fix to handle booleans mapping to union type
1 parent 2e48ec8 commit aeeb0ae

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

src/convert/unresolved-types-mapper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ export default class UnresolvedTypesMapper {
105105
}
106106
// otherwise, do nothing, mapped already contains filtered list
107107

108+
if (this.containsBoolean(type)) {
109+
// replace true | false with boolean
110+
const indexTrue = mapped.findIndex(t => t instanceof IntrinsicType && t.name === 'true');
111+
const indexFalse = mapped.findIndex(t => t instanceof IntrinsicType && t.name === 'false');
112+
const firstIndex = Math.min(indexTrue, indexFalse);
113+
const lastIndex = Math.max(indexTrue, indexFalse);
114+
115+
mapped.splice(firstIndex, 1, new IntrinsicType('boolean'));
116+
mapped.splice(lastIndex, 1);
117+
}
118+
108119
// get unique list of types from remapped types
109120
const unique: Type[] = [];
110121
mapped.forEach(item => {
@@ -143,6 +154,13 @@ export default class UnresolvedTypesMapper {
143154
return this.isUnmapped(type) ? new UnknownType('unknown') : type;
144155
}
145156

157+
private containsBoolean(type: UnionType): boolean {
158+
return (
159+
!!type.types.find(t => t instanceof IntrinsicType && t.name === 'true')
160+
&& !!type.types.find(t => t instanceof IntrinsicType && t.name === 'false')
161+
);
162+
}
163+
146164
private getReflectionsByInstanceType<T extends Reflection>(project: ProjectReflection, func: { new(...args: any[]): T }): T[] {
147165
return Object.values(project.reflections).filter(r => r instanceof func) as unknown as T[];
148166
}

tests/test-data/type/expected.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ declare type StatusTypes = {
77
* Failure
88
*/
99
"all-bad": string;
10-
}
10+
}
11+
12+
declare type OptionalBoolean = boolean | undefined;

tests/test-data/type/input.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export type StatusTypes = {
88
*/
99
'all-bad': string;
1010
}
11+
12+
export type OptionalBoolean = boolean | undefined;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare interface Optionals {
2+
myBool?: boolean;
3+
myString?: string;
4+
myNumber?: number;
5+
otherBool1?: boolean;
6+
otherBool2?: boolean;
7+
otherBoolUnion1?: boolean | string;
8+
otherBoolUnion2?: string | boolean;
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare interface Optionals {
2+
myBool?: boolean;
3+
myString?: string;
4+
myNumber?: number;
5+
6+
otherBool1?: true | false;
7+
otherBool2?: false | true;
8+
otherBoolUnion1?: false | string | true;
9+
otherBoolUnion2?: string | true | false | undefined;
10+
}

0 commit comments

Comments
 (0)