Skip to content

More actionable "must have annotation" message #35839

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

Merged
merged 4 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18931,7 +18931,7 @@ namespace ts {
return getTypeOfSymbol(symbol);
}
if (diagnostic && symbol.valueDeclaration) {
addRelatedInfo(diagnostic, createDiagnosticForNode(symbol.valueDeclaration, Diagnostics._0_is_declared_here, symbolToString(symbol)));
addRelatedInfo(diagnostic, createDiagnosticForNode(symbol.valueDeclaration, Diagnostics._0_needs_an_explicit_type_annotation, symbolToString(symbol)));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,10 @@
"category": "Error",
"code": 2781
},
"'{0}' needs an explicit type annotation.": {
"category": "Message",
"code": 2782
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/assertionTypePredicates1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(149,5): error TS
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:144:11: 'assert' is declared here.
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:144:11: 'assert' needs an explicit type annotation.
Copy link
Member

Choose a reason for hiding this comment

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

as far as I understand, this error was previously incorrect and is now still incorrect. the real problem is that we can't use asserts with arrow functions, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, the arrow function is fully annotated, but the assert declaration itself needs to be annotated. It's very stupid.

- const assert = (value: unknown): asserts value => { }
+ const assert: (value: unknown) => asserts value = (value: unknown): asserts value => { }

Copy link
Member Author

Choose a reason for hiding this comment

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

Or "better"

- const assert = (value: unknown): asserts value => { }
+ const assert: (value: unknown) => asserts value = value => { }

const a = [assert];
a[0](typeof x === "string"); // Error
~~~~
Expand All @@ -193,7 +193,7 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(149,5): error TS
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:148:11: 't1' is declared here.
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:148:11: 't1' needs an explicit type annotation.
const t2: Test = new Test();
t2.assert(typeof x === "string");
}
Expand Down