Issue implicit any errors for mapped types without annotations (#21104)

* Fixed #21011

* Updated code of merged message

* Reversed message code change and moved error to it's appropriate position

* Applied suggested improvements

* Fixed wrong diagnostics message in checker

* Reverted diagnostic message change
This commit is contained in:
Stanislav Iliev 2018-01-10 03:11:57 +02:00 committed by Mohamed Hegazy
parent 8bce69e6bd
commit 03fd77657d
7 changed files with 36 additions and 1 deletions

View File

@ -11281,6 +11281,9 @@ namespace ts {
}
diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type;
break;
case SyntaxKind.MappedType:
error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type);
return;
default:
diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type;
}
@ -20312,6 +20315,11 @@ namespace ts {
function checkMappedType(node: MappedTypeNode) {
checkSourceElement(node.typeParameter);
checkSourceElement(node.type);
if (noImplicitAny && !node.type) {
reportImplicitAnyError(node, anyType);
}
const type = <MappedType>getTypeFromMappedTypeNode(node);
const constraintType = getConstraintTypeFromMappedType(type);
checkTypeAssignableTo(constraintType, stringType, node.typeParameter.constraint);

View File

@ -3571,7 +3571,10 @@
"category": "Error",
"code": 7038
},
"Mapped object type implicitly has an 'any' template type.": {
"category": "Error",
"code": 7039
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000

View File

@ -0,0 +1,7 @@
tests/cases/compiler/anyMappedTypesError.ts(1,12): error TS7039: Mapped object type implicitly has an 'any' template type.
==== tests/cases/compiler/anyMappedTypesError.ts (1 errors) ====
type Foo = {[P in "bar"]};
~~~~~~~~~~~~~~
!!! error TS7039: Mapped object type implicitly has an 'any' template type.

View File

@ -0,0 +1,4 @@
//// [anyMappedTypesError.ts]
type Foo = {[P in "bar"]};
//// [anyMappedTypesError.js]

View File

@ -0,0 +1,5 @@
=== tests/cases/compiler/anyMappedTypesError.ts ===
type Foo = {[P in "bar"]};
>Foo : Symbol(Foo, Decl(anyMappedTypesError.ts, 0, 0))
>P : Symbol(P, Decl(anyMappedTypesError.ts, 0, 13))

View File

@ -0,0 +1,5 @@
=== tests/cases/compiler/anyMappedTypesError.ts ===
type Foo = {[P in "bar"]};
>Foo : Foo
>P : P

View File

@ -0,0 +1,3 @@
// @noImplicitAny: true
type Foo = {[P in "bar"]};