Skip to content

Commit 214ef0c

Browse files
committed
No did-you-mean-to-call error on casts
I chose to do the ad-hoc check rather than yet another tree walk. 1. It's faster to run and easier to read. 2. This error came from looking at real code. It happened twice, so I think the best estimate for other uses that happened zero times is in fact zero. 3. I couldn't think of other places to put the cast, given the restrictions on `testedNode` just before the new code.
1 parent 1c25b00 commit 214ef0c

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34554,8 +34554,11 @@ namespace ts {
3455434554
: isPropertyAccessExpression(location) ? location.name
3455534555
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
3455634556
: undefined;
34557+
const isPropertyExpressionCast = isPropertyAccessExpression(location)
34558+
&& isParenthesizedExpression(location.expression)
34559+
&& isAssertionExpression(location.expression.expression);
3455734560

34558-
if (!testedNode) {
34561+
if (!testedNode || isPropertyExpressionCast) {
3455934562
return;
3456034563
}
3456134564

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [truthinessCallExpressionCoercion3.ts]
2+
// from #41640, based on an example in ant-design
3+
interface I {
4+
always(): void
5+
}
6+
7+
function f(result: unknown) {
8+
if ((result as I).always) {
9+
return result
10+
}
11+
}
12+
13+
14+
//// [truthinessCallExpressionCoercion3.js]
15+
function f(result) {
16+
if (result.always) {
17+
return result;
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/truthinessCallExpressionCoercion3.ts ===
2+
// from #41640, based on an example in ant-design
3+
interface I {
4+
>I : Symbol(I, Decl(truthinessCallExpressionCoercion3.ts, 0, 0))
5+
6+
always(): void
7+
>always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
8+
}
9+
10+
function f(result: unknown) {
11+
>f : Symbol(f, Decl(truthinessCallExpressionCoercion3.ts, 3, 1))
12+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
13+
14+
if ((result as I).always) {
15+
>(result as I).always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
16+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
17+
>I : Symbol(I, Decl(truthinessCallExpressionCoercion3.ts, 0, 0))
18+
>always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
19+
20+
return result
21+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
22+
}
23+
}
24+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/truthinessCallExpressionCoercion3.ts ===
2+
// from #41640, based on an example in ant-design
3+
interface I {
4+
always(): void
5+
>always : () => void
6+
}
7+
8+
function f(result: unknown) {
9+
>f : (result: unknown) => unknown
10+
>result : unknown
11+
12+
if ((result as I).always) {
13+
>(result as I).always : () => void
14+
>(result as I) : I
15+
>result as I : I
16+
>result : unknown
17+
>always : () => void
18+
19+
return result
20+
>result : unknown
21+
}
22+
}
23+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strictNullChecks: true
2+
// @lib: esnext,dom
3+
4+
// from #41640, based on an example in ant-design
5+
interface I {
6+
always(): void
7+
}
8+
9+
function f(result: unknown) {
10+
if ((result as I).always) {
11+
return result
12+
}
13+
}

0 commit comments

Comments
 (0)