Skip to content

Commit 7f64b3a

Browse files
TypeScript Botsandersn
TypeScript Bot
andauthored
Cherry-pick PR #42626 into release-4.2 (#42780)
Component commits: 214ef0c 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. 1d34778 Merge branch 'master' into no-did-you-mean-to-call-error-on-casts Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 980b273 commit 7f64b3a

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
@@ -34542,8 +34542,11 @@ namespace ts {
3454234542
: isPropertyAccessExpression(location) ? location.name
3454334543
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
3454434544
: undefined;
34545+
const isPropertyExpressionCast = isPropertyAccessExpression(location)
34546+
&& isParenthesizedExpression(location.expression)
34547+
&& isAssertionExpression(location.expression.expression);
3454534548

34546-
if (!testedNode) {
34549+
if (!testedNode || isPropertyExpressionCast) {
3454734550
return;
3454834551
}
3454934552

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)