Skip to content

Commit a1ac23c

Browse files
ericzhangjxGerrit0
authored andcommitted
fix: Support for type operators readonly & unique
1 parent f67f8db commit a1ac23c

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/lib/converter/types/type-operator.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ export class TypeOperatorConverter extends ConverterTypeComponent implements Typ
1111
*/
1212
priority = 50;
1313

14+
/**
15+
* add two more operators based on the references below:
16+
* https://github.com/microsoft/TypeScript/blob/e83102134e5640abb78b0c62941b3b5003ab6c1a/src/compiler/types.ts#L1602
17+
* https://github.com/TypeStrong/typedoc/blob/b7a5b2d5ea1ae088e9510783ede20e842b120d0f/src/lib/converter/types.ts#L375
18+
*/
19+
private readonly supportedOperatorNames = {
20+
[ts.SyntaxKind.KeyOfKeyword]: 'keyof',
21+
[ts.SyntaxKind.UniqueKeyword]: 'unique',
22+
[ts.SyntaxKind.ReadonlyKeyword]: 'readonly'
23+
} as const;
24+
1425
/**
1526
* Test whether this converter can handle the given TypeScript node.
1627
*/
@@ -28,7 +39,8 @@ export class TypeOperatorConverter extends ConverterTypeComponent implements Typ
2839
convertNode(context: Context, node: ts.TypeOperatorNode): TypeOperatorType | undefined {
2940
const target = this.owner.convertType(context, node.type);
3041
if (target) {
31-
return new TypeOperatorType(target);
42+
const operator = this.supportedOperatorNames[node.operator];
43+
return new TypeOperatorType(target, operator);
3244
}
3345
}
3446
}

src/lib/models/types/type-operator.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ export class TypeOperatorType extends Type {
1414
*/
1515
readonly type = 'typeOperator';
1616

17-
target: Type;
18-
19-
// currently, there is only one type operator, this is always "keyof"
20-
// but, if more types will be added in the future we are ready.
21-
readonly operator = 'keyof';
22-
23-
constructor(target: Type) {
17+
constructor(public target: Type, public operator: 'keyof' | 'unique' | 'readonly') {
2418
super();
25-
this.target = target;
2619
}
2720

2821
/**
@@ -31,7 +24,7 @@ export class TypeOperatorType extends Type {
3124
* @return A clone of this type.
3225
*/
3326
clone(): Type {
34-
return new TypeOperatorType(this.target.clone());
27+
return new TypeOperatorType(this.target.clone(), this.operator);
3528
}
3629

3730
/**
@@ -45,7 +38,7 @@ export class TypeOperatorType extends Type {
4538
return false;
4639
}
4740

48-
return type.target.equals(this.target);
41+
return type instanceof TypeOperatorType && type.operator === this.operator && type.target.equals(this.target);
4942
}
5043

5144
/**

0 commit comments

Comments
 (0)