Skip to content

Commit 15dae3f

Browse files
committed
Add tests
1 parent 8f847c5 commit 15dae3f

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
tests/cases/compiler/implicitConstParameters.ts(39,27): error TS2532: Object is possibly 'undefined'.
2+
tests/cases/compiler/implicitConstParameters.ts(45,27): error TS2532: Object is possibly 'undefined'.
3+
4+
5+
==== tests/cases/compiler/implicitConstParameters.ts (2 errors) ====
6+
7+
function doSomething(cb: () => void) {
8+
cb();
9+
}
10+
11+
function fn(x: number | string) {
12+
if (typeof x === 'number') {
13+
doSomething(() => x.toFixed());
14+
}
15+
}
16+
17+
function f1(x: string | undefined) {
18+
if (!x) {
19+
return;
20+
}
21+
doSomething(() => x.length);
22+
}
23+
24+
function f2(x: string | undefined) {
25+
if (x) {
26+
doSomething(() => {
27+
doSomething(() => x.length);
28+
});
29+
}
30+
}
31+
32+
function f3(x: string | undefined) {
33+
inner();
34+
function inner() {
35+
if (x) {
36+
doSomething(() => x.length);
37+
}
38+
}
39+
}
40+
41+
function f4(x: string | undefined) {
42+
x = "abc"; // causes x to be considered non-const
43+
if (x) {
44+
doSomething(() => x.length);
45+
~
46+
!!! error TS2532: Object is possibly 'undefined'.
47+
}
48+
}
49+
50+
function f5(x: string | undefined) {
51+
if (x) {
52+
doSomething(() => x.length);
53+
~
54+
!!! error TS2532: Object is possibly 'undefined'.
55+
}
56+
x = "abc"; // causes x to be considered non-const
57+
}
58+
59+
60+
function f6(x: string | undefined) {
61+
const y = x || "";
62+
if (x) {
63+
doSomething(() => y.length);
64+
}
65+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//// [implicitConstParameters.ts]
2+
3+
function doSomething(cb: () => void) {
4+
cb();
5+
}
6+
7+
function fn(x: number | string) {
8+
if (typeof x === 'number') {
9+
doSomething(() => x.toFixed());
10+
}
11+
}
12+
13+
function f1(x: string | undefined) {
14+
if (!x) {
15+
return;
16+
}
17+
doSomething(() => x.length);
18+
}
19+
20+
function f2(x: string | undefined) {
21+
if (x) {
22+
doSomething(() => {
23+
doSomething(() => x.length);
24+
});
25+
}
26+
}
27+
28+
function f3(x: string | undefined) {
29+
inner();
30+
function inner() {
31+
if (x) {
32+
doSomething(() => x.length);
33+
}
34+
}
35+
}
36+
37+
function f4(x: string | undefined) {
38+
x = "abc"; // causes x to be considered non-const
39+
if (x) {
40+
doSomething(() => x.length);
41+
}
42+
}
43+
44+
function f5(x: string | undefined) {
45+
if (x) {
46+
doSomething(() => x.length);
47+
}
48+
x = "abc"; // causes x to be considered non-const
49+
}
50+
51+
52+
function f6(x: string | undefined) {
53+
const y = x || "";
54+
if (x) {
55+
doSomething(() => y.length);
56+
}
57+
}
58+
59+
//// [implicitConstParameters.js]
60+
function doSomething(cb) {
61+
cb();
62+
}
63+
function fn(x) {
64+
if (typeof x === 'number') {
65+
doSomething(function () { return x.toFixed(); });
66+
}
67+
}
68+
function f1(x) {
69+
if (!x) {
70+
return;
71+
}
72+
doSomething(function () { return x.length; });
73+
}
74+
function f2(x) {
75+
if (x) {
76+
doSomething(function () {
77+
doSomething(function () { return x.length; });
78+
});
79+
}
80+
}
81+
function f3(x) {
82+
inner();
83+
function inner() {
84+
if (x) {
85+
doSomething(function () { return x.length; });
86+
}
87+
}
88+
}
89+
function f4(x) {
90+
x = "abc"; // causes x to be considered non-const
91+
if (x) {
92+
doSomething(function () { return x.length; });
93+
}
94+
}
95+
function f5(x) {
96+
if (x) {
97+
doSomething(function () { return x.length; });
98+
}
99+
x = "abc"; // causes x to be considered non-const
100+
}
101+
function f6(x) {
102+
var y = x || "";
103+
if (x) {
104+
doSomething(function () { return y.length; });
105+
}
106+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// @strictNullChecks: true
2+
3+
function doSomething(cb: () => void) {
4+
cb();
5+
}
6+
7+
function fn(x: number | string) {
8+
if (typeof x === 'number') {
9+
doSomething(() => x.toFixed());
10+
}
11+
}
12+
13+
function f1(x: string | undefined) {
14+
if (!x) {
15+
return;
16+
}
17+
doSomething(() => x.length);
18+
}
19+
20+
function f2(x: string | undefined) {
21+
if (x) {
22+
doSomething(() => {
23+
doSomething(() => x.length);
24+
});
25+
}
26+
}
27+
28+
function f3(x: string | undefined) {
29+
inner();
30+
function inner() {
31+
if (x) {
32+
doSomething(() => x.length);
33+
}
34+
}
35+
}
36+
37+
function f4(x: string | undefined) {
38+
x = "abc"; // causes x to be considered non-const
39+
if (x) {
40+
doSomething(() => x.length);
41+
}
42+
}
43+
44+
function f5(x: string | undefined) {
45+
if (x) {
46+
doSomething(() => x.length);
47+
}
48+
x = "abc"; // causes x to be considered non-const
49+
}
50+
51+
52+
function f6(x: string | undefined) {
53+
const y = x || "";
54+
if (x) {
55+
doSomething(() => y.length);
56+
}
57+
}

0 commit comments

Comments
 (0)