Skip to content

Commit

Permalink
fix: Support for type operators readonly & unique
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhangjx authored and Gerrit0 committed Aug 28, 2020
1 parent f67f8db commit a1ac23c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/lib/converter/types/type-operator.ts
Expand Up @@ -11,6 +11,17 @@ export class TypeOperatorConverter extends ConverterTypeComponent implements Typ
*/
priority = 50;

/**
* add two more operators based on the references below:
* https://github.com/microsoft/TypeScript/blob/e83102134e5640abb78b0c62941b3b5003ab6c1a/src/compiler/types.ts#L1602
* https://github.com/TypeStrong/typedoc/blob/b7a5b2d5ea1ae088e9510783ede20e842b120d0f/src/lib/converter/types.ts#L375
*/
private readonly supportedOperatorNames = {
[ts.SyntaxKind.KeyOfKeyword]: 'keyof',
[ts.SyntaxKind.UniqueKeyword]: 'unique',
[ts.SyntaxKind.ReadonlyKeyword]: 'readonly'
} as const;

/**
* Test whether this converter can handle the given TypeScript node.
*/
Expand All @@ -28,7 +39,8 @@ export class TypeOperatorConverter extends ConverterTypeComponent implements Typ
convertNode(context: Context, node: ts.TypeOperatorNode): TypeOperatorType | undefined {
const target = this.owner.convertType(context, node.type);
if (target) {
return new TypeOperatorType(target);
const operator = this.supportedOperatorNames[node.operator];
return new TypeOperatorType(target, operator);
}
}
}
13 changes: 3 additions & 10 deletions src/lib/models/types/type-operator.ts
Expand Up @@ -14,15 +14,8 @@ export class TypeOperatorType extends Type {
*/
readonly type = 'typeOperator';

target: Type;

// currently, there is only one type operator, this is always "keyof"
// but, if more types will be added in the future we are ready.
readonly operator = 'keyof';

constructor(target: Type) {
constructor(public target: Type, public operator: 'keyof' | 'unique' | 'readonly') {
super();
this.target = target;
}

/**
Expand All @@ -31,7 +24,7 @@ export class TypeOperatorType extends Type {
* @return A clone of this type.
*/
clone(): Type {
return new TypeOperatorType(this.target.clone());
return new TypeOperatorType(this.target.clone(), this.operator);
}

/**
Expand All @@ -45,7 +38,7 @@ export class TypeOperatorType extends Type {
return false;
}

return type.target.equals(this.target);
return type instanceof TypeOperatorType && type.operator === this.operator && type.target.equals(this.target);
}

/**
Expand Down

0 comments on commit a1ac23c

Please sign in to comment.