Skip to content

Commit 153e881

Browse files
committed
feat: extract & expose toString for stringifying types
Closes TypeStrong#1160
1 parent 0399e75 commit 153e881

20 files changed

+285
-149
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { Application } from './lib/application';
22
export { CliApplication } from './lib/cli';
3+
export * from './lib/stringifiers';
34

45
export { EventDispatcher, Event } from './lib/utils/events';
56
export { createMinimatch } from './lib/utils/paths';

src/lib/models/types/abstract.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stringifyType } from '../../stringifiers';
2+
13
/**
24
* Base class of all type definitions.
35
*
@@ -31,7 +33,7 @@ export abstract class Type {
3133
* Return a string representation of this type.
3234
*/
3335
toString(): string {
34-
return 'void';
36+
return this.type === 'void' ? 'void' : stringifyType(this);
3537
}
3638

3739
/**

src/lib/models/types/array.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type, UnionType, IntersectionType } from './index';
1+
import { Type } from './index';
22

33
/**
44
* Represents an array type.
@@ -50,16 +50,4 @@ export class ArrayType extends Type {
5050
}
5151
return type.elementType.equals(this.elementType);
5252
}
53-
54-
/**
55-
* Return a string representation of this type.
56-
*/
57-
toString() {
58-
const elementTypeStr = this.elementType.toString();
59-
if (this.elementType instanceof UnionType || this.elementType instanceof IntersectionType) {
60-
return '(' + elementTypeStr + ')[]';
61-
} else {
62-
return elementTypeStr + '[]';
63-
}
64-
}
6553
}

src/lib/models/types/conditional.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,4 @@ export class ConditionalType extends Type {
4747
this.trueType.equals(type.trueType) &&
4848
this.falseType.equals(type.falseType);
4949
}
50-
51-
/**
52-
* Return a string representation of this type.
53-
*/
54-
toString() {
55-
return this.checkType + ' extends ' + this.extendsType + ' ? ' + this.trueType + ' : ' + this.falseType;
56-
}
5750
}

src/lib/models/types/indexed-access.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,4 @@ export class IndexedAccessType extends Type {
3939
}
4040
return type.objectType.equals(this.objectType) && type.indexType.equals(this.indexType);
4141
}
42-
43-
/**
44-
* Return a string representation of this type.
45-
*/
46-
toString() {
47-
return `${this.objectType.toString()}[${this.indexType.toString()}]`;
48-
}
4942
}

src/lib/models/types/inferred.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,4 @@ export class InferredType extends Type {
3838
}
3939
return this.name === type.name;
4040
}
41-
42-
/**
43-
* Return a string representation of this type.
44-
*/
45-
toString() {
46-
return `infer ${this.name}`;
47-
}
4841
}

src/lib/models/types/intersection.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,4 @@ export class IntersectionType extends Type {
4949
}
5050
return Type.isTypeListSimilar(type.types, this.types);
5151
}
52-
53-
/**
54-
* Return a string representation of this type.
55-
*/
56-
toString() {
57-
const names: string[] = [];
58-
this.types.forEach((element) => {
59-
names.push(element.toString());
60-
});
61-
62-
return names.join(' & ');
63-
}
6452
}

src/lib/models/types/intrinsic.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,4 @@ export class IntrinsicType extends Type {
4747
return type instanceof IntrinsicType &&
4848
type.name === this.name;
4949
}
50-
51-
/**
52-
* Return a string representation of this type.
53-
*/
54-
toString() {
55-
return this.name;
56-
}
5750
}

src/lib/models/types/predicate.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,4 @@ export class PredicateType extends Type {
7373
&& this.asserts === type.asserts
7474
&& (this.targetType?.equals(type.targetType!) ?? true);
7575
}
76-
77-
/**
78-
* Return a string representation of this type.
79-
*/
80-
toString() {
81-
const out = this.asserts ? ['asserts', this.name] : [this.name];
82-
if (this.targetType) {
83-
out.push('is', this.targetType.toString());
84-
}
85-
86-
return out.join(' ');
87-
}
8876
}

src/lib/models/types/query.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,4 @@ export class QueryType extends Type {
2525
equals(other: Type) {
2626
return other instanceof QueryType && this.queryType.equals(other.queryType);
2727
}
28-
29-
toString() {
30-
return `typeof ${this.queryType.toString()}`;
31-
}
3228
}

0 commit comments

Comments
 (0)