Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No did-you-mean-to-call error on casts #42626

Merged
merged 2 commits into from Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Expand Up @@ -34554,8 +34554,11 @@ namespace ts {
: isPropertyAccessExpression(location) ? location.name
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
: undefined;
const isPropertyExpressionCast = isPropertyAccessExpression(location)
&& isParenthesizedExpression(location.expression)
&& isAssertionExpression(location.expression.expression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is weird that adding a layer of parens will throw this off. Maybe use skipParentheses

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea


if (!testedNode) {
if (!testedNode || isPropertyExpressionCast) {
return;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/truthinessCallExpressionCoercion3.js
@@ -0,0 +1,19 @@
//// [truthinessCallExpressionCoercion3.ts]
// from #41640, based on an example in ant-design
interface I {
always(): void
}

function f(result: unknown) {
if ((result as I).always) {
return result
}
}


//// [truthinessCallExpressionCoercion3.js]
function f(result) {
if (result.always) {
return result;
}
}
@@ -0,0 +1,24 @@
=== tests/cases/compiler/truthinessCallExpressionCoercion3.ts ===
// from #41640, based on an example in ant-design
interface I {
>I : Symbol(I, Decl(truthinessCallExpressionCoercion3.ts, 0, 0))

always(): void
>always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
}

function f(result: unknown) {
>f : Symbol(f, Decl(truthinessCallExpressionCoercion3.ts, 3, 1))
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))

if ((result as I).always) {
>(result as I).always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
>I : Symbol(I, Decl(truthinessCallExpressionCoercion3.ts, 0, 0))
>always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))

return result
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
}
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/truthinessCallExpressionCoercion3.types
@@ -0,0 +1,23 @@
=== tests/cases/compiler/truthinessCallExpressionCoercion3.ts ===
// from #41640, based on an example in ant-design
interface I {
always(): void
>always : () => void
}

function f(result: unknown) {
>f : (result: unknown) => unknown
>result : unknown

if ((result as I).always) {
>(result as I).always : () => void
>(result as I) : I
>result as I : I
>result : unknown
>always : () => void

return result
>result : unknown
}
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/truthinessCallExpressionCoercion3.ts
@@ -0,0 +1,13 @@
// @strictNullChecks: true
// @lib: esnext,dom

// from #41640, based on an example in ant-design
interface I {
always(): void
}

function f(result: unknown) {
if ((result as I).always) {
return result
}
}