Skip to content

Commit 7d82c22

Browse files
committed
Add comments + minor changes
1 parent 582d8b8 commit 7d82c22

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/compiler/checker.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace ts {
106106
isOptionalParameter
107107
};
108108

109-
const tupleTypes: TupleType[] = [];
109+
const tupleTypes: GenericType[] = [];
110110
const unionTypes = createMap<UnionType>();
111111
const intersectionTypes = createMap<IntersectionType>();
112112
const stringLiteralTypes = createMap<LiteralType>();
@@ -2213,7 +2213,7 @@ namespace ts {
22132213
}
22142214
else if (type.target.flags & TypeFlags.Tuple) {
22152215
writePunctuation(writer, SyntaxKind.OpenBracketToken);
2216-
writeTypeList(type.typeArguments.slice(0, type.target.typeParameters.length), SyntaxKind.CommaToken);
2216+
writeTypeList(type.typeArguments.slice(0, getTypeReferenceArity(type)), SyntaxKind.CommaToken);
22172217
writePunctuation(writer, SyntaxKind.CloseBracketToken);
22182218
}
22192219
else {
@@ -4978,6 +4978,10 @@ namespace ts {
49784978
return type;
49794979
}
49804980

4981+
function getTypeReferenceArity(type: TypeReference): number {
4982+
return type.target.typeParameters.length;
4983+
}
4984+
49814985
// Get type from reference to class or interface
49824986
function getTypeFromClassOrInterfaceReference(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference, symbol: Symbol): Type {
49834987
const type = <InterfaceType>getDeclaredTypeOfSymbol(getMergedSymbol(symbol));
@@ -5220,7 +5224,14 @@ namespace ts {
52205224
return links.resolvedType;
52215225
}
52225226

5223-
function createTupleTypeOfArity(arity: number): TupleType {
5227+
// We represent tuple types as type references to synthesized generic interface types created by
5228+
// this function. The types are of the form:
5229+
//
5230+
// interface Tuple<T0, T1, T2, ...> extends Array<T0 | T1 | T2 | ...> { 0: T0, 1: T1, 2: T2, ... }
5231+
//
5232+
// Note that the generic type created by this function has no symbol associated with it. The same
5233+
// is true for each of the synthesized type parameters.
5234+
function createTupleTypeOfArity(arity: number): GenericType {
52245235
const typeParameters: TypeParameter[] = [];
52255236
const properties: Symbol[] = [];
52265237
for (let i = 0; i < arity; i++) {
@@ -5230,7 +5241,7 @@ namespace ts {
52305241
property.type = typeParameter;
52315242
properties.push(property);
52325243
}
5233-
const type = <TupleType & InterfaceTypeWithDeclaredMembers>createObjectType(TypeFlags.Tuple | TypeFlags.Reference);
5244+
const type = <GenericType & InterfaceTypeWithDeclaredMembers>createObjectType(TypeFlags.Tuple | TypeFlags.Reference);
52345245
type.typeParameters = typeParameters;
52355246
type.outerTypeParameters = undefined;
52365247
type.localTypeParameters = typeParameters;
@@ -5248,14 +5259,10 @@ namespace ts {
52485259
return type;
52495260
}
52505261

5251-
function getTupleTypeOfArity(arity: number): TupleType {
5262+
function getTupleTypeOfArity(arity: number): GenericType {
52525263
return tupleTypes[arity] || (tupleTypes[arity] = createTupleTypeOfArity(arity));
52535264
}
52545265

5255-
function getTypeReferenceArity(type: TypeReference): number {
5256-
return type.target.typeParameters.length;
5257-
}
5258-
52595266
function createTupleType(elementTypes: Type[]) {
52605267
return createTypeReference(getTupleTypeOfArity(elementTypes.length), elementTypes);
52615268
}

src/compiler/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ namespace ts {
22632263
Class = 1 << 15, // Class
22642264
Interface = 1 << 16, // Interface
22652265
Reference = 1 << 17, // Generic type reference
2266-
Tuple = 1 << 18, // Tuple
2266+
Tuple = 1 << 18, // Synthesized generic tuple type
22672267
Union = 1 << 19, // Union (T | U)
22682268
Intersection = 1 << 20, // Intersection (T & U)
22692269
Anonymous = 1 << 21, // Anonymous
@@ -2385,8 +2385,6 @@ namespace ts {
23852385
instantiations: Map<TypeReference>; // Generic instantiation cache
23862386
}
23872387

2388-
export interface TupleType extends GenericType { }
2389-
23902388
export interface UnionOrIntersectionType extends Type {
23912389
types: Type[]; // Constituent types
23922390
/* @internal */

0 commit comments

Comments
 (0)