/
type-operator.ts
50 lines (44 loc) · 1.17 KB
/
type-operator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Type } from './abstract';
/**
* Represents a type operator type.
*
* ~~~
* class A {}
* class B<T extends keyof A> {}
* ~~~
*/
export class TypeOperatorType extends Type {
/**
* The type name identifier.
*/
readonly type = 'typeOperator';
constructor(public target: Type, public operator: 'keyof' | 'unique' | 'readonly') {
super();
}
/**
* Clone this type.
*
* @return A clone of this type.
*/
clone(): Type {
return new TypeOperatorType(this.target.clone(), this.operator);
}
/**
* Test whether this type equals the given type.
*
* @param type The type that should be checked for equality.
* @returns TRUE if the given type equals this type, FALSE otherwise.
*/
equals(type: TypeOperatorType): boolean {
if (!(type instanceof TypeOperatorType)) {
return false;
}
return type instanceof TypeOperatorType && type.operator === this.operator && type.target.equals(this.target);
}
/**
* Return a string representation of this type.
*/
toString() {
return `${this.operator} ${this.target.toString()}`;
}
}