Skip to content

Commit

Permalink
Remove grammar restriction and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Aug 30, 2018
1 parent 4174ab9 commit 2c47d31
Show file tree
Hide file tree
Showing 54 changed files with 3,267 additions and 482 deletions.
36 changes: 17 additions & 19 deletions src/compiler/checker.ts
Expand Up @@ -30,6 +30,13 @@ namespace ts {
(preserveConstEnums && moduleState === ModuleInstanceState.ConstEnumOnly);
}

function getIndexSignatureParameterErrorChain() {
return chainDiagnosticMessages(
/*details*/ undefined,
Diagnostics.An_index_signature_parameter_type_must_be_assignable_to_string_number_symbol
);
}

export function createTypeChecker(host: TypeCheckerHost, produceDiagnostics: boolean): TypeChecker {
const getPackagesSet: () => Map<true> = memoize(() => {
const set = createMap<true>();
Expand Down Expand Up @@ -9444,6 +9451,10 @@ namespace ts {
if (isGenericMappedType(objectType)) {
return type.simplified = substituteIndexedMappedType(objectType, type);
}
const indexedType = getResultingTypeOfIndexOnType(objectType, indexType);
if (indexedType) {
return type.simplified = getSimplifiedType(indexedType);
}
if (objectType.flags & TypeFlags.TypeParameter) {
const constraint = getConstraintOfTypeParameter(objectType as TypeParameter);
if (constraint && isGenericMappedType(constraint)) {
Expand Down Expand Up @@ -11081,7 +11092,7 @@ namespace ts {
if (s & TypeFlags.Null && (!strictNullChecks || t & TypeFlags.Null)) return true;
if (s & TypeFlags.Object && t & TypeFlags.NonPrimitive) return true;
if (s & TypeFlags.UniqueESSymbol || t & TypeFlags.UniqueESSymbol) return false;
if (relation === assignableRelation || relation === definitelyAssignableRelation || relation === comparableRelation || relation === indexAssignableRelation || relation === indexAccessAssignableRelation) {
if (relation === assignableRelation || relation === definitelyAssignableRelation || relation === comparableRelation) {
if (s & TypeFlags.Any) return true;
// Type number or any numeric literal type is assignable to any numeric enum type or any
// numeric enum literal type. This rule exists for backwards compatibility reasons because
Expand All @@ -11090,10 +11101,12 @@ namespace ts {
t & TypeFlags.Enum || t & TypeFlags.NumberLiteral && t & TypeFlags.EnumLiteral)) return true;
}
if (relation === indexAccessAssignableRelation) {
if (s & TypeFlags.Any) return true;
if (s & TypeFlags.NumberLike && t & TypeFlags.String) return true;
if (s & TypeFlags.NumberLiteral && t & TypeFlags.StringLiteral) return (source as LiteralType).value === +(target as LiteralType).value;
}
if (relation === indexAssignableRelation) {
if (s & TypeFlags.Any) return true;
if (s & TypeFlags.String && t & TypeFlags.Number) return true;
if (s & TypeFlags.StringLiteral && t & TypeFlags.NumberLiteral) return +(source as LiteralType).value === (target as LiteralType).value;
}
Expand Down Expand Up @@ -18938,7 +18951,7 @@ namespace ts {

function getArrayifiedType(type: Type) {
if (forEachType(type, t => !(t.flags & (TypeFlags.Any | TypeFlags.Instantiable) || isArrayType(t) || isTupleType(t)))) {
return createArrayType(getIndexTypeOfType(type, IndexKind.Number) || errorType);
return createArrayType(getResultingTypeOfIndexOnType(type, numberType) || errorType);
}
return type;
}
Expand Down Expand Up @@ -29011,23 +29024,8 @@ namespace ts {
if (!parameter.type) {
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation);
}
if (parameter.type.kind !== SyntaxKind.StringKeyword && parameter.type.kind !== SyntaxKind.NumberKeyword) {
const type = getTypeFromTypeNode(parameter.type);

if (type.flags & TypeFlags.String || type.flags & TypeFlags.Number) {
return grammarErrorOnNode(parameter.name,
Diagnostics.An_index_signature_parameter_type_cannot_be_a_type_alias_Consider_writing_0_Colon_1_Colon_2_instead,
getTextOfNode(parameter.name),
typeToString(type),
typeToString(getTypeFromTypeNode(node.type!)));
}

if (type.flags & TypeFlags.Union && allTypesAssignableToKind(type, TypeFlags.StringLiteral, /*strict*/ true)) {
return grammarErrorOnNode(parameter.name,
Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead);
}

return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_or_number);
else {
checkTypeAssignableTo(getTypeFromTypeNode(parameter.type), stringNumberSymbolType, parameter.name, /*headMessage*/ undefined, getIndexSignatureParameterErrorChain);
}
if (!node.type) {
return grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_a_type_annotation);
Expand Down
10 changes: 1 addition & 9 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -71,7 +71,7 @@
"category": "Error",
"code": 1022
},
"An index signature parameter type must be 'string' or 'number'.": {
"An index signature parameter type must be assignable to 'string | number | symbol'.": {
"category": "Error",
"code": 1023
},
Expand Down Expand Up @@ -955,14 +955,6 @@
"category": "Error",
"code": 1335
},
"An index signature parameter type cannot be a type alias. Consider writing '[{0}: {1}]: {2}' instead.": {
"category": "Error",
"code": 1336
},
"An index signature parameter type cannot be a union type. Consider using a mapped object type instead.": {
"category": "Error",
"code": 1337
},
"'infer' declarations are only permitted in the 'extends' clause of a conditional type.": {
"category": "Error",
"code": 1338
Expand Down
56 changes: 0 additions & 56 deletions src/services/codefixes/convertToMappedObjectType.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/services/tsconfig.json
Expand Up @@ -72,7 +72,6 @@
"codefixes/requireInTs.ts",
"codefixes/useDefaultImport.ts",
"codefixes/fixAddModuleReferTypeMissingTypeof.ts",
"codefixes/convertToMappedObjectType.ts",
"refactors/convertExport.ts",
"refactors/convertImport.ts",
"refactors/extractSymbol.ts",
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/arraySigChecking.errors.txt
@@ -1,4 +1,4 @@
tests/cases/compiler/arraySigChecking.ts(11,17): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/arraySigChecking.ts(11,16): error TS1021: An index signature must have a type annotation.
tests/cases/compiler/arraySigChecking.ts(18,5): error TS2322: Type 'void[]' is not assignable to type 'string[]'.
Type 'void' is not assignable to type 'string'.
tests/cases/compiler/arraySigChecking.ts(22,1): error TS2322: Type 'number[][]' is not assignable to type 'number[][][]'.
Expand All @@ -18,8 +18,8 @@ tests/cases/compiler/arraySigChecking.ts(22,1): error TS2322: Type 'number[][]'
}

var foo: { [index: any]; }; // expect an error here
~~~~~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
~~~~~~~~~~~~~
!!! error TS1021: An index signature must have a type annotation.
}

interface myInt {
Expand Down
@@ -1,13 +1,10 @@
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,13): error TS2304: Cannot find name 'TypeNotFound'.
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,13): error TS4092: Parameter 'index' of index signature from exported interface has or is using private name 'TypeNotFound'.


==== tests/cases/compiler/declarationEmitIndexTypeNotFound.ts (3 errors) ====
==== tests/cases/compiler/declarationEmitIndexTypeNotFound.ts (2 errors) ====
export interface Test {
[index: TypeNotFound]: any;
~~~~~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'TypeNotFound'.
~~~~~~~~~~~~
Expand Down

This file was deleted.

Expand Up @@ -2,7 +2,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(10,21): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,19): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(14,23): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
Expand All @@ -14,7 +13,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(26,30): error TS2314: Generic type 'E<T>' requires 1 type argument(s).


==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts (14 errors) ====
==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts (13 errors) ====
// it is an error to use a generic type without type arguments
// all of these are errors

Expand All @@ -35,8 +34,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
declare var d: { [x: C]: C };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
~
Expand Down
Expand Up @@ -2,7 +2,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(10,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(14,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
Expand All @@ -24,7 +23,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(37,10): error TS2314: Generic type 'E<T>' requires 1 type argument(s).


==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts (24 errors) ====
==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts (23 errors) ====
// it is an error to use a generic type without type arguments
// all of these are errors

Expand All @@ -45,8 +44,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
var d: { [x: C]: C };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
~
Expand Down
Expand Up @@ -2,7 +2,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(10,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(14,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
Expand All @@ -24,7 +23,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(37,10): error TS2314: Generic type 'E<T>' requires 1 type argument(s).


==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts (24 errors) ====
==== tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts (23 errors) ====
// it is an error to use a generic type without type arguments
// all of these are errors

Expand All @@ -45,8 +44,6 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
~
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
var d: { [x: I]: I };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
~
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
~
Expand Down

0 comments on commit 2c47d31

Please sign in to comment.