Skip to content

Commit b2f422e

Browse files
committed
add test
1 parent 0ae0c4b commit b2f422e

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//// [tests/cases/conformance/controlFlow/controlFlowInferFromExpressionsReturningBoolean2.ts] ////
2+
3+
=== controlFlowInferFromExpressionsReturningBoolean2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62279
5+
6+
export function assertCondition<T>(condition: T): asserts condition {
7+
>assertCondition : Symbol(assertCondition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 0, 0))
8+
>T : Symbol(T, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 2, 32))
9+
>condition : Symbol(condition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 2, 35))
10+
>T : Symbol(T, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 2, 32))
11+
>condition : Symbol(condition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 2, 35))
12+
13+
if (!condition) {
14+
>condition : Symbol(condition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 2, 35))
15+
16+
throw new Error();
17+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
18+
}
19+
}
20+
21+
type ValidType = "A" | "B";
22+
>ValidType : Symbol(ValidType, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 6, 1))
23+
24+
declare const status: ValidType;
25+
>status : Symbol(status, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 13))
26+
>ValidType : Symbol(ValidType, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 6, 1))
27+
28+
function test1() {
29+
>test1 : Symbol(test1, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 32))
30+
31+
assertCondition(status !== "B");
32+
>assertCondition : Symbol(assertCondition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 0, 0))
33+
>status : Symbol(status, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 13))
34+
35+
const status2 = status;
36+
>status2 : Symbol(status2, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 14, 7))
37+
>status : Symbol(status, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 13))
38+
39+
assertCondition(status2 === "A");
40+
>assertCondition : Symbol(assertCondition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 0, 0))
41+
>status2 : Symbol(status2, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 14, 7))
42+
}
43+
44+
function test2() {
45+
>test2 : Symbol(test2, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 16, 1))
46+
47+
assertCondition(status !== "B");
48+
>assertCondition : Symbol(assertCondition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 0, 0))
49+
>status : Symbol(status, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 13))
50+
51+
for (const _ of [1, 2]) {
52+
>_ : Symbol(_, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 20, 12))
53+
54+
const status2 = status;
55+
>status2 : Symbol(status2, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 21, 9))
56+
>status : Symbol(status, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 10, 13))
57+
58+
assertCondition(status2 === "A");
59+
>assertCondition : Symbol(assertCondition, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 0, 0))
60+
>status2 : Symbol(status2, Decl(controlFlowInferFromExpressionsReturningBoolean2.ts, 21, 9))
61+
}
62+
}
63+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//// [tests/cases/conformance/controlFlow/controlFlowInferFromExpressionsReturningBoolean2.ts] ////
2+
3+
=== controlFlowInferFromExpressionsReturningBoolean2.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62279
5+
6+
export function assertCondition<T>(condition: T): asserts condition {
7+
>assertCondition : <T>(condition: T) => asserts condition
8+
> : ^ ^^ ^^ ^^^^^
9+
>condition : T
10+
> : ^
11+
12+
if (!condition) {
13+
>!condition : boolean
14+
> : ^^^^^^^
15+
>condition : T
16+
> : ^
17+
18+
throw new Error();
19+
>new Error() : Error
20+
> : ^^^^^
21+
>Error : ErrorConstructor
22+
> : ^^^^^^^^^^^^^^^^
23+
}
24+
}
25+
26+
type ValidType = "A" | "B";
27+
>ValidType : ValidType
28+
> : ^^^^^^^^^
29+
30+
declare const status: ValidType;
31+
>status : ValidType
32+
> : ^^^^^^^^^
33+
34+
function test1() {
35+
>test1 : () => void
36+
> : ^^^^^^^^^^
37+
38+
assertCondition(status !== "B");
39+
>assertCondition(status !== "B") : void
40+
> : ^^^^
41+
>assertCondition : <T>(condition: T) => asserts condition
42+
> : ^ ^^ ^^ ^^^^^
43+
>status !== "B" : boolean
44+
> : ^^^^^^^
45+
>status : ValidType
46+
> : ^^^^^^^^^
47+
>"B" : "B"
48+
> : ^^^
49+
50+
const status2 = status;
51+
>status2 : "A"
52+
> : ^^^
53+
>status : "A"
54+
> : ^^^
55+
56+
assertCondition(status2 === "A");
57+
>assertCondition(status2 === "A") : void
58+
> : ^^^^
59+
>assertCondition : <T>(condition: T) => asserts condition
60+
> : ^ ^^ ^^ ^^^^^
61+
>status2 === "A" : boolean
62+
> : ^^^^^^^
63+
>status2 : "A"
64+
> : ^^^
65+
>"A" : "A"
66+
> : ^^^
67+
}
68+
69+
function test2() {
70+
>test2 : () => void
71+
> : ^^^^^^^^^^
72+
73+
assertCondition(status !== "B");
74+
>assertCondition(status !== "B") : void
75+
> : ^^^^
76+
>assertCondition : <T>(condition: T) => asserts condition
77+
> : ^ ^^ ^^ ^^^^^
78+
>status !== "B" : boolean
79+
> : ^^^^^^^
80+
>status : ValidType
81+
> : ^^^^^^^^^
82+
>"B" : "B"
83+
> : ^^^
84+
85+
for (const _ of [1, 2]) {
86+
>_ : number
87+
> : ^^^^^^
88+
>[1, 2] : number[]
89+
> : ^^^^^^^^
90+
>1 : 1
91+
> : ^
92+
>2 : 2
93+
> : ^
94+
95+
const status2 = status;
96+
>status2 : "A"
97+
> : ^^^
98+
>status : "A"
99+
> : ^^^
100+
101+
assertCondition(status2 === "A");
102+
>assertCondition(status2 === "A") : void
103+
> : ^^^^
104+
>assertCondition : <T>(condition: T) => asserts condition
105+
> : ^ ^^ ^^ ^^^^^
106+
>status2 === "A" : boolean
107+
> : ^^^^^^^
108+
>status2 : "A"
109+
> : ^^^
110+
>"A" : "A"
111+
> : ^^^
112+
}
113+
}
114+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/62279
5+
6+
export function assertCondition<T>(condition: T): asserts condition {
7+
if (!condition) {
8+
throw new Error();
9+
}
10+
}
11+
12+
type ValidType = "A" | "B";
13+
14+
declare const status: ValidType;
15+
16+
function test1() {
17+
assertCondition(status !== "B");
18+
const status2 = status;
19+
assertCondition(status2 === "A");
20+
}
21+
22+
function test2() {
23+
assertCondition(status !== "B");
24+
for (const _ of [1, 2]) {
25+
const status2 = status;
26+
assertCondition(status2 === "A");
27+
}
28+
}

0 commit comments

Comments
 (0)