Skip to content
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

Index signature parameter type allows Enum and type alias = number|strin... #2652

Closed
wants to merge 15 commits into from
Closed
861 changes: 586 additions & 275 deletions bin/tsc.js

Large diffs are not rendered by default.

1,016 changes: 666 additions & 350 deletions bin/tsserver.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions bin/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ declare module "typescript" {
}
interface Identifier extends PrimaryExpression {
text: string;
originalKeywordKind?: SyntaxKind;
}
interface QualifiedName extends Node {
left: EntityName;
Expand Down Expand Up @@ -991,8 +992,10 @@ declare module "typescript" {
Anonymous = 32768,
ObjectLiteral = 131072,
ESSymbol = 1048576,
Subset = 2097152,
StringLike = 258,
NumberLike = 132,
SubsetMaybe = 4224,
ObjectType = 48128,
}
interface Type {
Expand All @@ -1010,8 +1013,8 @@ declare module "typescript" {
declaredProperties: Symbol[];
declaredCallSignatures: Signature[];
declaredConstructSignatures: Signature[];
declaredStringIndexType: Type;
declaredNumberIndexType: Type;
declaredStringIndex: IndexType;
declaredNumberIndex: IndexType;
}
interface TypeReference extends ObjectType {
target: GenericType;
Expand All @@ -1026,6 +1029,19 @@ declare module "typescript" {
interface UnionType extends Type {
types: Type[];
}
enum IndexAlphaNumeric {
NO = 0,
YES = 1,
INHERITED = 2,
}
interface IndexType {
kind: IndexKind;
typeOfValue: Type;
typeOfIndex?: Type;
declaredNode?: SignatureDeclaration;
declaredCount?: number;
inherited?: Symbol;
}
interface TypeParameter extends Type {
constraint: Type;
}
Expand Down
966 changes: 661 additions & 305 deletions bin/typescript.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions bin/typescriptServices.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ declare module ts {
}
interface Identifier extends PrimaryExpression {
text: string;
originalKeywordKind?: SyntaxKind;
}
interface QualifiedName extends Node {
left: EntityName;
Expand Down Expand Up @@ -991,8 +992,10 @@ declare module ts {
Anonymous = 32768,
ObjectLiteral = 131072,
ESSymbol = 1048576,
Subset = 2097152,
StringLike = 258,
NumberLike = 132,
SubsetMaybe = 4224,
ObjectType = 48128,
}
interface Type {
Expand All @@ -1010,8 +1013,8 @@ declare module ts {
declaredProperties: Symbol[];
declaredCallSignatures: Signature[];
declaredConstructSignatures: Signature[];
declaredStringIndexType: Type;
declaredNumberIndexType: Type;
declaredStringIndex: IndexType;
declaredNumberIndex: IndexType;
}
interface TypeReference extends ObjectType {
target: GenericType;
Expand All @@ -1026,6 +1029,19 @@ declare module ts {
interface UnionType extends Type {
types: Type[];
}
enum IndexAlphaNumeric {
NO = 0,
YES = 1,
INHERITED = 2,
}
interface IndexType {
kind: IndexKind;
typeOfValue: Type;
typeOfIndex?: Type;
declaredNode?: SignatureDeclaration;
declaredCount?: number;
inherited?: Symbol;
}
interface TypeParameter extends Type {
constraint: Type;
}
Expand Down
966 changes: 661 additions & 305 deletions bin/typescriptServices.js

Large diffs are not rendered by default.

662 changes: 428 additions & 234 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ts {
An_index_signature_parameter_cannot_have_an_initializer: { code: 1020, category: DiagnosticCategory.Error, key: "An index signature parameter cannot have an initializer." },
An_index_signature_must_have_a_type_annotation: { code: 1021, category: DiagnosticCategory.Error, key: "An index signature must have a type annotation." },
An_index_signature_parameter_must_have_a_type_annotation: { code: 1022, category: DiagnosticCategory.Error, key: "An index signature parameter must have a type annotation." },
An_index_signature_parameter_type_must_be_string_or_number: { code: 1023, category: DiagnosticCategory.Error, key: "An index signature parameter type must be 'string' or 'number'." },
An_index_signature_parameter_type_must_be_string_number_or_an_enum_type: { code: 1023, category: DiagnosticCategory.Error, key: "An index signature parameter type must be 'string', 'number', or an enum type." },
Copy link
Contributor

Choose a reason for hiding this comment

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

Must be 'string', 'number' or an enum type.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I'm missing the difference, unless you want to take out the Oxford comma, in which case I think it's more helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

Some of the error messages show: "An index signature parameter type must be 'string', 'number' or Enum"

Copy link
Member

Choose a reason for hiding this comment

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

Ah - the baselines need to be updated since this change.

A_class_or_interface_declaration_can_only_have_one_extends_clause: { code: 1024, category: DiagnosticCategory.Error, key: "A class or interface declaration can only have one 'extends' clause." },
An_extends_clause_must_precede_an_implements_clause: { code: 1025, category: DiagnosticCategory.Error, key: "An 'extends' clause must precede an 'implements' clause." },
A_class_can_only_extend_a_single_class: { code: 1026, category: DiagnosticCategory.Error, key: "A class can only extend a single class." },
Expand Down Expand Up @@ -203,8 +203,8 @@ module ts {
Types_of_property_0_are_incompatible: { code: 2326, category: DiagnosticCategory.Error, key: "Types of property '{0}' are incompatible." },
Property_0_is_optional_in_type_1_but_required_in_type_2: { code: 2327, category: DiagnosticCategory.Error, key: "Property '{0}' is optional in type '{1}' but required in type '{2}'." },
Types_of_parameters_0_and_1_are_incompatible: { code: 2328, category: DiagnosticCategory.Error, key: "Types of parameters '{0}' and '{1}' are incompatible." },
Index_signature_is_missing_in_type_0: { code: 2329, category: DiagnosticCategory.Error, key: "Index signature is missing in type '{0}'." },
Index_signatures_are_incompatible: { code: 2330, category: DiagnosticCategory.Error, key: "Index signatures are incompatible." },
Missing_0_index_signature_in_type_1: { code: 2329, category: DiagnosticCategory.Error, key: "Missing {0} index signature in type '{1}'." },
Index_signatures_0_and_1_are_incompatible: { code: 2330, category: DiagnosticCategory.Error, key: "Index signatures '{0}' and '{1}' are incompatible." },
this_cannot_be_referenced_in_a_module_body: { code: 2331, category: DiagnosticCategory.Error, key: "'this' cannot be referenced in a module body." },
this_cannot_be_referenced_in_current_location: { code: 2332, category: DiagnosticCategory.Error, key: "'this' cannot be referenced in current location." },
this_cannot_be_referenced_in_constructor_arguments: { code: 2333, category: DiagnosticCategory.Error, key: "'this' cannot be referenced in constructor arguments." },
Expand Down Expand Up @@ -279,9 +279,9 @@ module ts {
Setters_cannot_return_a_value: { code: 2408, category: DiagnosticCategory.Error, key: "Setters cannot return a value." },
Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: DiagnosticCategory.Error, key: "Return type of constructor signature must be assignable to the instance type of the class" },
All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: DiagnosticCategory.Error, key: "All symbols within a 'with' block will be resolved to 'any'." },
Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: DiagnosticCategory.Error, key: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." },
Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: DiagnosticCategory.Error, key: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." },
Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: DiagnosticCategory.Error, key: "Numeric index type '{0}' is not assignable to string index type '{1}'." },
Property_0_of_type_1_is_not_assignable_to_string_index_2: { code: 2411, category: DiagnosticCategory.Error, key: "Property '{0}' of type '{1}' is not assignable to string index '{2}'." },
Property_0_of_type_1_is_not_assignable_to_numeric_index_2: { code: 2412, category: DiagnosticCategory.Error, key: "Property '{0}' of type '{1}' is not assignable to numeric index '{2}'." },
Numeric_index_0_is_not_assignable_to_string_index_1: { code: 2413, category: DiagnosticCategory.Error, key: "Numeric index '{0}' is not assignable to string index '{1}'." },
Class_name_cannot_be_0: { code: 2414, category: DiagnosticCategory.Error, key: "Class name cannot be '{0}'" },
Class_0_incorrectly_extends_base_class_1: { code: 2415, category: DiagnosticCategory.Error, key: "Class '{0}' incorrectly extends base class '{1}'." },
Class_static_side_0_incorrectly_extends_base_class_static_side_1: { code: 2417, category: DiagnosticCategory.Error, key: "Class static side '{0}' incorrectly extends base class static side '{1}'." },
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"category": "Error",
"code": 1022
},
"An index signature parameter type must be 'string' or 'number'.": {
"An index signature parameter type must be 'string', 'number', or an enum type.": {
"category": "Error",
"code": 1023
},
Expand Down Expand Up @@ -799,11 +799,11 @@
"category": "Error",
"code": 2328
},
"Index signature is missing in type '{0}'.": {
"Missing {0} index signature in type '{1}'.": {
"category": "Error",
"code": 2329
},
"Index signatures are incompatible.": {
"Index signatures '{0}' and '{1}' are incompatible.": {
"category": "Error",
"code": 2330
},
Expand Down Expand Up @@ -1103,15 +1103,15 @@
"category": "Error",
"code": 2410
},
"Property '{0}' of type '{1}' is not assignable to string index type '{2}'.": {
"Property '{0}' of type '{1}' is not assignable to string index '{2}'.": {
"category": "Error",
"code": 2411
},
"Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'.": {
"Property '{0}' of type '{1}' is not assignable to numeric index '{2}'.": {
"category": "Error",
"code": 2412
},
"Numeric index type '{0}' is not assignable to string index type '{1}'.": {
"Numeric index '{0}' is not assignable to string index '{1}'.": {
"category": "Error",
"code": 2413
},
Expand Down
41 changes: 30 additions & 11 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,15 +1446,17 @@ module ts {
/* @internal */
ContainsUndefinedOrNull = 0x00040000, // Type is or contains Undefined or Null type
/* @internal */
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
ESSymbol = 0x00100000, // Type of symbol primitive introduced in ES6
Subset = 0x00200000, // Type that has a subset of valid values
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this mean? can you elaborate?


/* @internal */
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
/* @internal */
Primitive = String | Number | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum,
StringLike = String | StringLiteral,
NumberLike = Number | Enum,
SubsetMaybe = Enum | Reference,
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
/* @internal */
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
Expand Down Expand Up @@ -1488,8 +1490,8 @@ module ts {
declaredProperties: Symbol[]; // Declared members
declaredCallSignatures: Signature[]; // Declared call signatures
declaredConstructSignatures: Signature[]; // Declared construct signatures
declaredStringIndexType: Type; // Declared string index type
declaredNumberIndexType: Type; // Declared numeric index type
declaredStringIndex: IndexType; // Declared string type
declaredNumberIndex: IndexType; // Declared numeric type
}

// Type references (TypeFlags.Reference)
Expand All @@ -1515,15 +1517,32 @@ module ts {
resolvedProperties: SymbolTable; // Cache of resolved properties
}

/* @internal */
// Resolved object or union type
export enum IndexAlphaNumeric {
NO,
YES, // string & number indexes come from different types
INHERITED // string & number indexes are both from an inherited type
}

export interface IndexType {
kind: IndexKind // Kind of index
typeOfValue: Type // any
typeOfIndex?: Type // string|number|enum

// Useful for error reporting
declaredNode?: SignatureDeclaration, // Declaration of [x: typeOfIndex]: typeOfValue
declaredCount?: number // Number of declarations
inherited?: Symbol // Symbol of baseType where inherited
}

/* @internal */ // Resolved object or union type
export interface ResolvedType extends ObjectType, UnionType {
members: SymbolTable; // Properties by name
properties: Symbol[]; // Properties
callSignatures: Signature[]; // Call signatures of type
constructSignatures: Signature[]; // Construct signatures of type
stringIndexType: Type; // String index type
numberIndexType: Type; // Numeric index type
members: SymbolTable; // Properties by name
properties: Symbol[]; // Properties
callSignatures: Signature[]; // Call signatures of type
constructSignatures: Signature[]; // Construct signatures of type
stringIndex: IndexType; // String index type
numberIndex: IndexType; // Number index type
alphaNumericIndex?: IndexAlphaNumeric // Information about alphanumeric index
}

// Type parameters (TypeFlags.TypeParameter)
Expand Down
Loading