-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 all 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 |
---|---|---|
|
@@ -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: Assertions require every name in the call target to be declared with an explicit type annotation. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(131,5): error TS2776: Assertions require the call target to be an identifier or qualified name. | ||
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(133,5): error TS2775: Assertions require every name in the call target to be 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,23 @@ 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: Assertions require every name in the call target to be declared with an explicit type annotation. | ||
!!! related TS2728 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:128:11: 'assert' is declared here. | ||
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.
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. It would, but do we really want yet another diagnostic? 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. I know you already merged this, but for the future, diagnostics are cheap! They bear very little overhead for the <20 of us on the core team compared to at least hundreds of thousands of TypeScript users. 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. We discussed in the team room and agreed that the existing message is fine, so I didn't change it. |
||
const a = [assert]; | ||
a[0](typeof x === "string"); // Error | ||
~~~~ | ||
!!! error TS2776: Assertions require the call target to be an identifier or qualified name. | ||
const t1 = new Test(); | ||
t1.assert(typeof x === "string"); // Error | ||
~~~~~~~~~ | ||
!!! error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation. | ||
!!! related TS2728 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:132:11: 't1' is declared here. | ||
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.
BTW, will this need to be updated for optional chains? Should a
t1?.assert(b)
be a thing?