Skip to content

Commit b975dfa

Browse files
authored
Write more useful types in .types test outputs (#48578)
1 parent 4d2fb54 commit b975dfa

File tree

428 files changed

+1197
-1189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

428 files changed

+1197
-1189
lines changed

Diff for: src/harness/typeWriter.ts

+33-25
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,39 @@ namespace Harness {
115115
// let type = this.checker.getTypeAtLocation(node);
116116
let type = ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) ? this.checker.getTypeAtLocation(node.parent) : undefined;
117117
if (!type || type.flags & ts.TypeFlags.Any) type = this.checker.getTypeAtLocation(node);
118-
const typeString =
119-
// Distinguish `errorType`s from `any`s; but only if the file has no errors.
120-
// Additionally,
121-
// * the LHS of a qualified name
122-
// * a binding pattern name
123-
// * labels
124-
// * the "global" in "declare global"
125-
// * the "target" in "new.target"
126-
// * names in import statements
127-
// * type-only names in export statements
128-
// * and intrinsic jsx tag names
129-
// return `error`s via `getTypeAtLocation`
130-
// But this is generally expected, so we don't call those out, either
131-
(!this.hadErrorBaseline &&
132-
type.flags & ts.TypeFlags.Any &&
133-
!ts.isBindingElement(node.parent) &&
134-
!ts.isPropertyAccessOrQualifiedName(node.parent) &&
135-
!ts.isLabelName(node) &&
136-
!(ts.isModuleDeclaration(node.parent) && ts.isGlobalScopeAugmentation(node.parent)) &&
137-
!ts.isMetaProperty(node.parent) &&
138-
!this.isImportStatementName(node) &&
139-
!this.isExportStatementName(node) &&
140-
!this.isIntrinsicJsxTag(node)) ?
141-
(type as ts.IntrinsicType).intrinsicName :
142-
this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
118+
// Distinguish `errorType`s from `any`s; but only if the file has no errors.
119+
// Additionally,
120+
// * the LHS of a qualified name
121+
// * a binding pattern name
122+
// * labels
123+
// * the "global" in "declare global"
124+
// * the "target" in "new.target"
125+
// * names in import statements
126+
// * type-only names in export statements
127+
// * and intrinsic jsx tag names
128+
// return `error`s via `getTypeAtLocation`
129+
// But this is generally expected, so we don't call those out, either
130+
let typeString: string;
131+
if (!this.hadErrorBaseline &&
132+
type.flags & ts.TypeFlags.Any &&
133+
!ts.isBindingElement(node.parent) &&
134+
!ts.isPropertyAccessOrQualifiedName(node.parent) &&
135+
!ts.isLabelName(node) &&
136+
!(ts.isModuleDeclaration(node.parent) && ts.isGlobalScopeAugmentation(node.parent)) &&
137+
!ts.isMetaProperty(node.parent) &&
138+
!this.isImportStatementName(node) &&
139+
!this.isExportStatementName(node) &&
140+
!this.isIntrinsicJsxTag(node)) {
141+
typeString = (type as ts.IntrinsicType).intrinsicName;
142+
}
143+
else {
144+
typeString = this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
145+
if (ts.isIdentifier(node) && ts.isTypeAliasDeclaration(node.parent) && node.parent.name === node && typeString === ts.idText(node)) {
146+
// for a complex type alias `type T = ...`, showing "T : T" isn't very helpful for type tests. When the type produced is the same as
147+
// the name of the type alias, recreate the type string without reusing the alias name
148+
typeString = this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType | ts.TypeFormatFlags.InTypeAlias);
149+
}
150+
}
143151
return {
144152
line: lineAndCharacter.line,
145153
syntaxKind: node.kind,

Diff for: tests/baselines/reference/DeclarationErrorsNoEmitOnError.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/DeclarationErrorsNoEmitOnError.ts ===
22
type T = { x : number }
3-
>T : T
3+
>T : { x: number; }
44
>x : number
55

66
export interface I {

Diff for: tests/baselines/reference/abstractClassUnionInstantiation.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ abstract class AbstractB { b: string; }
1414
>b : string
1515

1616
type Abstracts = typeof AbstractA | typeof AbstractB;
17-
>Abstracts : Abstracts
17+
>Abstracts : typeof AbstractA | typeof AbstractB
1818
>AbstractA : typeof AbstractA
1919
>AbstractB : typeof AbstractB
2020

2121
type Concretes = typeof ConcreteA | typeof ConcreteB;
22-
>Concretes : Concretes
22+
>Concretes : typeof ConcreteA | typeof ConcreteB
2323
>ConcreteA : typeof ConcreteA
2424
>ConcreteB : typeof ConcreteB
2525

2626
type ConcretesOrAbstracts = Concretes | Abstracts;
27-
>ConcretesOrAbstracts : ConcretesOrAbstracts
27+
>ConcretesOrAbstracts : Abstracts | Concretes
2828

2929
declare const cls1: ConcretesOrAbstracts;
3030
>cls1 : ConcretesOrAbstracts

Diff for: tests/baselines/reference/accessorBodyInTypeContext.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/accessorBodyInTypeContext.ts ===
22
type A = {
3-
>A : A
3+
>A : { readonly foo: number; }
44

55
get foo() { return 0 }
66
>foo : number
@@ -9,7 +9,7 @@ type A = {
99
};
1010

1111
type B = {
12-
>B : B
12+
>B : { foo: any; }
1313

1414
set foo(v: any) { }
1515
>foo : any

Diff for: tests/baselines/reference/aliasOfGenericFunctionWithRestBehavedSameAsUnaliased.types

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ type ExtendedMapper<HandledInputT, OutputT, ArgsT extends any[]> = (name : strin
88
>args : ArgsT
99

1010
type a = ExtendedMapper<any, any, [any]>;
11-
>a : a
11+
>a : (name: string, mixed: any, args_0: any) => any
1212

1313
type b = ExtendedMapper<any, any, any[]>;
14-
>b : b
14+
>b : (name: string, mixed: any, ...args: any[]) => any
1515

1616
type test = a extends b ? "y" : "n"
1717
>test : "y"
@@ -32,10 +32,10 @@ type ExtendedMapper1<HandledInputT, OutputT, ArgsT extends any[]> = (
3232
);
3333

3434
type a1 = ExtendedMapper1<any, any, [any]>;
35-
>a1 : a1
35+
>a1 : (name: string, mixed: any, args_0: any) => any
3636

3737
type b1 = ExtendedMapper1<any, any, any[]>;
38-
>b1 : b1
38+
>b1 : (name: string, mixed: any, ...args: any[]) => any
3939

4040
type test1 = a1 extends b1 ? "y" : "n"
4141
>test1 : "y"
@@ -56,10 +56,10 @@ type ExtendedMapper2<HandledInputT, OutputT, ArgsT extends any[]> = (
5656
);
5757

5858
type a2 = ExtendedMapper2<any, any, [any]>;
59-
>a2 : a2
59+
>a2 : (name: string, mixed: any, args_0: any) => any
6060

6161
type b2 = ExtendedMapper2<any, any, any[]>;
62-
>b2 : b2
62+
>b2 : (name: string, mixed: any, ...args: any[]) => any
6363

6464
type test2 = a2 extends b2 ? "y" : "n"
6565
>test2 : "y"
@@ -69,13 +69,13 @@ let check2: test2 = "y";
6969
>"y" : "y"
7070

7171
type a3 = (name: string, mixed: any, args_0: any) => any
72-
>a3 : a3
72+
>a3 : (name: string, mixed: any, args_0: any) => any
7373
>name : string
7474
>mixed : any
7575
>args_0 : any
7676

7777
type b3 = (name: string, mixed: any, ...args: any[]) => any
78-
>b3 : b3
78+
>b3 : (name: string, mixed: any, ...args: any[]) => any
7979
>name : string
8080
>mixed : any
8181
>args : any[]

Diff for: tests/baselines/reference/anyMappedTypesError.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
=== tests/cases/compiler/anyMappedTypesError.ts ===
22
type Foo = {[P in "bar"]};
3-
>Foo : Foo
3+
>Foo : { bar: any; }
44

Diff for: tests/baselines/reference/argumentsAsPropertyName.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/compiler/argumentsAsPropertyName.ts ===
22
// target: es5
33
type MyType = {
4-
>MyType : MyType
4+
>MyType : { arguments: Array<string>; }
55

66
arguments: Array<string>
77
>arguments : string[]

Diff for: tests/baselines/reference/arrayDestructuringInSwitch1.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== tests/cases/compiler/arrayDestructuringInSwitch1.ts ===
22
export type Expression = BooleanLogicExpression | 'true' | 'false';
3-
>Expression : Expression
3+
>Expression : BooleanLogicExpression | "true" | "false"
44

55
export type BooleanLogicExpression = ['and', ...Expression[]] | ['not', Expression];
6-
>BooleanLogicExpression : BooleanLogicExpression
6+
>BooleanLogicExpression : ["and", ...Expression[]] | ["not", Expression]
77

88
export function evaluate(expression: Expression): boolean {
99
>evaluate : (expression: Expression) => boolean

Diff for: tests/baselines/reference/arrayDestructuringInSwitch2.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/arrayDestructuringInSwitch2.ts ===
22
type X = { kind: "a", a: [1] } | { kind: "b", a: [] }
3-
>X : X
3+
>X : { kind: "a"; a: [1]; } | { kind: "b"; a: []; }
44
>kind : "a"
55
>a : [1]
66
>kind : "b"

Diff for: tests/baselines/reference/assertionFunctionsCanNarrowByDiscriminant.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Dog {
1818
}
1919

2020
type Animal = Cat | Dog;
21-
>Animal : Animal
21+
>Animal : Cat | Dog
2222

2323
declare function assertEqual<T>(value: any, type: T): asserts value is T;
2424
>assertEqual : <T>(value: any, type: T) => asserts value is T

Diff for: tests/baselines/reference/assignmentCompatWithDiscriminatedUnion.types

+21-21
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Example1 {
66
>Example1 : typeof Example1
77

88
type S = { done: boolean, value: number };
9-
>S : S
9+
>S : { done: boolean; value: number; }
1010
>done : boolean
1111
>value : number
1212

1313
type T =
14-
>T : T
14+
>T : { done: true; value: number; } | { done: false; value: number; }
1515

1616
| { done: true, value: number } // T0
1717
>done : true
@@ -42,12 +42,12 @@ namespace Example2 {
4242
>Example2 : typeof Example2
4343

4444
type S = { a: 0 | 2, b: 4 };
45-
>S : S
45+
>S : { a: 0 | 2; b: 4; }
4646
>a : 0 | 2
4747
>b : 4
4848

4949
type T = { a: 0, b: 1 | 4 } // T0
50-
>T : T
50+
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2; } | { a: 2; b: 3 | 4; }
5151
>a : 0
5252
>b : 4 | 1
5353

@@ -78,12 +78,12 @@ namespace Example3 {
7878
>Example3 : typeof Example3
7979

8080
type S = { a: 0 | 2, b: 4 };
81-
>S : S
81+
>S : { a: 0 | 2; b: 4; }
8282
>a : 0 | 2
8383
>b : 4
8484

8585
type T = { a: 0, b: 1 | 4 } // T0
86-
>T : T
86+
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2 | 4; } | { a: 2; b: 3; }
8787
>a : 0
8888
>b : 4 | 1
8989

@@ -115,12 +115,12 @@ namespace Example4 {
115115
>Example4 : typeof Example4
116116

117117
type S = { a: 0 | 2, b: 4 };
118-
>S : S
118+
>S : { a: 0 | 2; b: 4; }
119119
>a : 0 | 2
120120
>b : 4
121121

122122
type T = { a: 0, b: 1 | 4 } // T0
123-
>T : T
123+
>T : { a: 0; b: 1 | 4; } | { a: 1; b: 2; } | { a: 2; b: 3 | 4; c: string; }
124124
>a : 0
125125
>b : 4 | 1
126126

@@ -155,16 +155,16 @@ namespace Example5 {
155155
// 3 discriminant properties with 3 types a piece
156156
// is 27 possible combinations.
157157
type N = 0 | 1 | 2;
158-
>N : N
158+
>N : 0 | 2 | 1
159159

160160
type S = { a: N, b: N, c: N };
161-
>S : S
161+
>S : { a: N; b: N; c: N; }
162162
>a : N
163163
>b : N
164164
>c : N
165165

166166
type T = { a: 0, b: N, c: N }
167-
>T : T
167+
>T : { a: 0; b: N; c: N; } | { a: 1; b: N; c: N; } | { a: 2; b: N; c: N; } | { a: N; b: 0; c: N; } | { a: N; b: 1; c: N; } | { a: N; b: 2; c: N; } | { a: N; b: N; c: 0; } | { a: N; b: N; c: 1; } | { a: N; b: N; c: 2; }
168168
>a : 0
169169
>b : N
170170
>c : N
@@ -228,7 +228,7 @@ namespace GH14865 {
228228
>GH14865 : typeof GH14865
229229

230230
type Style1 = {
231-
>Style1 : Style1
231+
>Style1 : { type: "A"; data: string; } | { type: "B"; data: string; }
232232

233233
type: "A";
234234
>type : "A"
@@ -246,7 +246,7 @@ namespace GH14865 {
246246
};
247247

248248
type Style2 = {
249-
>Style2 : Style2
249+
>Style2 : { type: "A" | "B"; data: string; }
250250

251251
type: "A" | "B";
252252
>type : "A" | "B"
@@ -322,10 +322,10 @@ namespace GH12052 {
322322
>type : "categorical"
323323

324324
type IAxis = ILinearAxis | ICategoricalAxis;
325-
>IAxis : IAxis
325+
>IAxis : ILinearAxis | ICategoricalAxis
326326

327327
type IAxisType = "linear" | "categorical";
328-
>IAxisType : IAxisType
328+
>IAxisType : "linear" | "categorical"
329329

330330
function getAxisType(): IAxisType {
331331
>getAxisType : () => IAxisType
@@ -381,10 +381,10 @@ namespace GH18421 {
381381
}
382382

383383
type ThingType = 'one' | 'two';
384-
>ThingType : ThingType
384+
>ThingType : "one" | "two"
385385

386386
type Thing = ThingTypeOne | ThingTypeTwo;
387-
>Thing : Thing
387+
>Thing : ThingTypeOne | ThingTypeTwo
388388

389389
function makeNewThing(thingType: ThingType): Thing {
390390
>makeNewThing : (thingType: ThingType) => Thing
@@ -406,7 +406,7 @@ namespace GH15907 {
406406
>GH15907 : typeof GH15907
407407

408408
type Action = { type: 'activate' } | { type: 'disactivate' };
409-
>Action : Action
409+
>Action : { type: 'activate'; } | { type: 'disactivate'; }
410410
>type : "activate"
411411
>type : "disactivate"
412412

@@ -445,7 +445,7 @@ namespace GH20889 {
445445
>type : "A2"
446446
}
447447
type AU = A1 | A2;
448-
>AU : AU
448+
>AU : A1 | A2
449449

450450
function foo(obj1: AU) {
451451
>foo : (obj1: AU) => void
@@ -470,10 +470,10 @@ namespace GH39357 {
470470
>GH39357 : typeof GH39357
471471

472472
type A = ["a", number] | ["b", number] | ["c", string];
473-
>A : A
473+
>A : ["a", number] | ["b", number] | ["c", string]
474474

475475
type B = "a" | "b" | "c";
476-
>B : B
476+
>B : "a" | "b" | "c"
477477

478478
declare const b: B;
479479
>b : B

Diff for: tests/baselines/reference/asyncFunctionContextuallyTypedReturns.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ g(async v => v ? "contextuallyTypable" : Promise.reject());
6464
>reject : <T = never>(reason?: any) => Promise<T>
6565

6666
type MyCallback = (thing: string) => void;
67-
>MyCallback : MyCallback
67+
>MyCallback : (thing: string) => void
6868
>thing : string
6969

7070
declare function h(cb: (v: boolean) => MyCallback | PromiseLike<MyCallback>): void;

Diff for: tests/baselines/reference/awaitedType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type T3 = Awaited<number | Promise<number>>;
99
>T3 : number
1010

1111
type T4 = Awaited<number | Promise<string>>;
12-
>T4 : T4
12+
>T4 : string | number
1313

1414
type T5 = Awaited<{ then: number }>;
1515
>T5 : { then: number; }

Diff for: tests/baselines/reference/awaitedTypeStrictNull.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type T3 = Awaited<number | Promise<number>>;
99
>T3 : number
1010

1111
type T4 = Awaited<number | Promise<string>>;
12-
>T4 : T4
12+
>T4 : string | number
1313

1414
type T5 = Awaited<{ then: number }>;
1515
>T5 : { then: number; }

0 commit comments

Comments
 (0)