Skip to content

Commit 05f51dc

Browse files
committed
Error when variable is circularly referenced in type annotation
1 parent cde3ed0 commit 05f51dc

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,12 +2278,19 @@ module ts {
22782278
}
22792279
}
22802280
else if (links.type === resolvingType) {
2281-
links.type = anyType;
2282-
if (compilerOptions.noImplicitAny) {
2283-
let diagnostic = (<VariableLikeDeclaration>symbol.valueDeclaration).type ?
2284-
Diagnostics._0_implicitly_has_type_any_because_it_is_referenced_directly_or_indirectly_in_its_own_type_annotation :
2285-
Diagnostics._0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer;
2286-
error(symbol.valueDeclaration, diagnostic, symbolToString(symbol));
2281+
if ((<VariableLikeDeclaration>symbol.valueDeclaration).type) {
2282+
// Variable has type annotation that circularly references the variable itself
2283+
links.type = unknownType;
2284+
error(symbol.valueDeclaration, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation,
2285+
symbolToString(symbol));
2286+
}
2287+
else {
2288+
// Variable has initializer that circularly references the variable itself
2289+
links.type = anyType;
2290+
if (compilerOptions.noImplicitAny) {
2291+
error(symbol.valueDeclaration, Diagnostics._0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer,
2292+
symbolToString(symbol));
2293+
}
22872294
}
22882295
}
22892296
return links.type;

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ module ts {
365365
An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2499, category: DiagnosticCategory.Error, key: "An interface can only extend an identifier/qualified-name with optional type arguments." },
366366
A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments: { code: 2500, category: DiagnosticCategory.Error, key: "A class can only implement an identifier/qualified-name with optional type arguments." },
367367
A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." },
368+
_0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." },
368369
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
369370
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
370371
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
@@ -516,7 +517,6 @@ module ts {
516517
Object_literal_s_property_0_implicitly_has_an_1_type: { code: 7018, category: DiagnosticCategory.Error, key: "Object literal's property '{0}' implicitly has an '{1}' type." },
517518
Rest_parameter_0_implicitly_has_an_any_type: { code: 7019, category: DiagnosticCategory.Error, key: "Rest parameter '{0}' implicitly has an 'any[]' type." },
518519
Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type: { code: 7020, category: DiagnosticCategory.Error, key: "Call signature, which lacks return-type annotation, implicitly has an 'any' return type." },
519-
_0_implicitly_has_type_any_because_it_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 7021, category: DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is referenced directly or indirectly in its own type annotation." },
520520
_0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer: { code: 7022, category: DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer." },
521521
_0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7023, category: DiagnosticCategory.Error, key: "'{0}' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." },
522522
Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7024, category: DiagnosticCategory.Error, key: "Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,10 @@
14471447
"category": "Error",
14481448
"code": 2501
14491449
},
1450+
"'{0}' is referenced directly or indirectly in its own type annotation.": {
1451+
"category": "Error",
1452+
"code": 2502
1453+
},
14501454

14511455
"Import declaration '{0}' is using private name '{1}'.": {
14521456
"category": "Error",
@@ -2055,10 +2059,6 @@
20552059
"category": "Error",
20562060
"code": 7020
20572061
},
2058-
"'{0}' implicitly has type 'any' because it is referenced directly or indirectly in its own type annotation.": {
2059-
"category": "Error",
2060-
"code": 7021
2061-
},
20622062
"'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer.": {
20632063
"category": "Error",
20642064
"code": 7022

0 commit comments

Comments
 (0)