-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Error when assertion function calls aren't CFA'd #33622
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
Changes from 4 commits
8619bff
894cc63
0595d61
a13f862
4648d6a
02395a9
56e0c6b
34f6e68
e5af71e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4062,6 +4062,12 @@ namespace ts { | |
return node.kind === SyntaxKind.Identifier || isPropertyAccessEntityNameExpression(node); | ||
} | ||
|
||
export function isDottedName(node: Expression): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, will this need to be updated for optional chains? Should a |
||
return node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.ThisKeyword || | ||
node.kind === SyntaxKind.PropertyAccessExpression && isDottedName((<PropertyAccessExpression>node).expression) || | ||
node.kind === SyntaxKind.ParenthesizedExpression && isDottedName((<ParenthesizedExpression>node).expression); | ||
} | ||
|
||
export function isPropertyAccessEntityNameExpression(node: Node): node is PropertyAccessEntityNameExpression { | ||
return isPropertyAccessExpression(node) && isEntityNameExpression(node.expression); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(121,15): error T | |
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(122,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(123,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(124,15): error TS1228: A type predicate is only allowed in return type position for functions and methods. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(129,5): error TS2775: Control flow effects of calls to assertion and never-returning functions are reflected only when every variable or property referenced in the function expression is declared with an explicit type annotation. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(131,5): error TS2776: Control flow effects of calls to assertion and never-returning functions are reflected only when the function expression is an identifier or qualified-name. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(133,5): error TS2775: Control flow effects of calls to assertion and never-returning functions are reflected only when every variable or property referenced in the function expression is declared with an explicit type annotation. | ||
|
||
|
||
==== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts (7 errors) ==== | ||
==== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts (10 errors) ==== | ||
declare function isString(value: unknown): value is string; | ||
declare function isArrayOfStrings(value: unknown): value is string[]; | ||
|
||
|
@@ -147,4 +150,21 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(124,15): error T | |
~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods. | ||
} | ||
|
||
function f20(x: unknown) { | ||
const assert = (value: unknown): asserts value => {} | ||
assert(typeof x === "string"); // Error | ||
~~~~~~ | ||
!!! error TS2775: Control flow effects of calls to assertion and never-returning functions are reflected only when every variable or property referenced in the function expression is declared with an explicit type annotation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which variable doesn't have an annotation? Can you give a related span? |
||
const a = [assert]; | ||
a[0](typeof x === "string"); // Error | ||
~~~~ | ||
!!! error TS2776: Control flow effects of calls to assertion and never-returning functions are reflected only when the function expression is an identifier or qualified-name. | ||
const t1 = new Test(); | ||
t1.assert(typeof x === "string"); // Error | ||
~~~~~~~~~ | ||
!!! error TS2775: Control flow effects of calls to assertion and never-returning functions are reflected only when every variable or property referenced in the function expression is declared with an explicit type annotation. | ||
const t2: Test = new Test(); | ||
t2.assert(typeof x === "string"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what this is telling a user. Is it something like the following?
Along with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasTypePredicateOrNeverReturnType(signature)
is true.