@@ -3612,7 +3612,8 @@ namespace ts {
3612
3612
createRestTypeNode(createArrayTypeNode(tupleConstituentNodes[i])) :
3613
3613
createOptionalTypeNode(tupleConstituentNodes[i]);
3614
3614
}
3615
- return createTupleTypeNode(tupleConstituentNodes);
3615
+ const tupleTypeNode = createTupleTypeNode(tupleConstituentNodes);
3616
+ return (<TupleType>type.target).readonly ? createTypeReferenceNode("Readonly", [tupleTypeNode]) : tupleTypeNode;
3616
3617
}
3617
3618
}
3618
3619
if (context.encounteredError || (context.flags & NodeBuilderFlags.AllowEmptyTuple)) {
@@ -5923,7 +5924,7 @@ namespace ts {
5923
5924
function getBaseTypes(type: InterfaceType): BaseType[] {
5924
5925
if (!type.resolvedBaseTypes) {
5925
5926
if (type.objectFlags & ObjectFlags.Tuple) {
5926
- type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters || emptyArray))];
5927
+ type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters || emptyArray), (<TupleType>type).readonly )];
5927
5928
}
5928
5929
else if (type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
5929
5930
if (type.symbol.flags & SymbolFlags.Class) {
@@ -7630,7 +7631,7 @@ namespace ts {
7630
7631
const typeVariable = getHomomorphicTypeVariable(type);
7631
7632
if (typeVariable) {
7632
7633
const constraint = getConstraintOfTypeParameter(typeVariable);
7633
- if (constraint && (isArrayType(constraint) || isReadonlyArrayType(constraint) || isTupleType(constraint))) {
7634
+ if (constraint && (isArrayType(constraint) || isTupleType(constraint))) {
7634
7635
const mapper = makeUnaryTypeMapper(typeVariable, constraint);
7635
7636
return instantiateType(type, combineTypeMappers(mapper, type.mapper));
7636
7637
}
@@ -9022,12 +9023,8 @@ namespace ts {
9022
9023
return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(/*reportErrors*/ true), [iteratedType]);
9023
9024
}
9024
9025
9025
- function createArrayType(elementType: Type): ObjectType {
9026
- return createTypeFromGenericGlobalType(globalArrayType, [elementType]);
9027
- }
9028
-
9029
- function createReadonlyArrayType(elementType: Type): ObjectType {
9030
- return createTypeFromGenericGlobalType(globalReadonlyArrayType, [elementType]);
9026
+ function createArrayType(elementType: Type, readonly?: boolean): ObjectType {
9027
+ return createTypeFromGenericGlobalType(readonly ? globalReadonlyArrayType : globalArrayType, [elementType]);
9031
9028
}
9032
9029
9033
9030
function getTypeFromArrayTypeNode(node: ArrayTypeNode): Type {
@@ -9045,7 +9042,7 @@ namespace ts {
9045
9042
//
9046
9043
// Note that the generic type created by this function has no symbol associated with it. The same
9047
9044
// is true for each of the synthesized type parameters.
9048
- function createTupleTypeOfArity(arity: number, minLength: number, hasRestElement: boolean, associatedNames: __String[] | undefined): TupleType {
9045
+ function createTupleTypeOfArity(arity: number, minLength: number, hasRestElement: boolean, readonly: boolean, associatedNames: __String[] | undefined): TupleType {
9049
9046
let typeParameters: TypeParameter[] | undefined;
9050
9047
const properties: Symbol[] = [];
9051
9048
const maxLength = hasRestElement ? arity - 1 : arity;
@@ -9054,7 +9051,8 @@ namespace ts {
9054
9051
for (let i = 0; i < arity; i++) {
9055
9052
const typeParameter = typeParameters[i] = createTypeParameter();
9056
9053
if (i < maxLength) {
9057
- const property = createSymbol(SymbolFlags.Property | (i >= minLength ? SymbolFlags.Optional : 0), "" + i as __String);
9054
+ const property = createSymbol(SymbolFlags.Property | (i >= minLength ? SymbolFlags.Optional : 0),
9055
+ "" + i as __String, readonly ? CheckFlags.Readonly : 0);
9058
9056
property.type = typeParameter;
9059
9057
properties.push(property);
9060
9058
}
@@ -9083,25 +9081,26 @@ namespace ts {
9083
9081
type.declaredNumberIndexInfo = undefined;
9084
9082
type.minLength = minLength;
9085
9083
type.hasRestElement = hasRestElement;
9084
+ type.readonly = readonly;
9086
9085
type.associatedNames = associatedNames;
9087
9086
return type;
9088
9087
}
9089
9088
9090
- function getTupleTypeOfArity(arity: number, minLength: number, hasRestElement: boolean, associatedNames?: __String[]): GenericType {
9091
- const key = arity + (hasRestElement ? "+" : ",") + minLength + (associatedNames && associatedNames.length ? "," + associatedNames.join(",") : "");
9089
+ function getTupleTypeOfArity(arity: number, minLength: number, hasRestElement: boolean, readonly: boolean, associatedNames?: __String[]): GenericType {
9090
+ const key = arity + (hasRestElement ? "+" : ",") + minLength + (readonly ? "R" : "") + ( associatedNames && associatedNames.length ? "," + associatedNames.join(",") : "");
9092
9091
let type = tupleTypes.get(key);
9093
9092
if (!type) {
9094
- tupleTypes.set(key, type = createTupleTypeOfArity(arity, minLength, hasRestElement, associatedNames));
9093
+ tupleTypes.set(key, type = createTupleTypeOfArity(arity, minLength, hasRestElement, readonly, associatedNames));
9095
9094
}
9096
9095
return type;
9097
9096
}
9098
9097
9099
- function createTupleType(elementTypes: ReadonlyArray<Type>, minLength = elementTypes.length, hasRestElement = false, associatedNames?: __String[]) {
9098
+ function createTupleType(elementTypes: ReadonlyArray<Type>, minLength = elementTypes.length, hasRestElement = false, readonly = false, associatedNames?: __String[]) {
9100
9099
const arity = elementTypes.length;
9101
9100
if (arity === 1 && hasRestElement) {
9102
9101
return createArrayType(elementTypes[0]);
9103
9102
}
9104
- const tupleType = getTupleTypeOfArity(arity, minLength, arity > 0 && hasRestElement, associatedNames);
9103
+ const tupleType = getTupleTypeOfArity(arity, minLength, arity > 0 && hasRestElement, readonly, associatedNames);
9105
9104
return elementTypes.length ? createTypeReference(tupleType, elementTypes) : tupleType;
9106
9105
}
9107
9106
@@ -9130,6 +9129,7 @@ namespace ts {
9130
9129
(type.typeArguments || emptyArray).slice(index),
9131
9130
Math.max(0, tuple.minLength - index),
9132
9131
tuple.hasRestElement,
9132
+ tuple.readonly,
9133
9133
tuple.associatedNames && tuple.associatedNames.slice(index),
9134
9134
);
9135
9135
}
@@ -10694,7 +10694,7 @@ namespace ts {
10694
10694
}
10695
10695
// Keep the flags from the symbol we're instantiating. Mark that is instantiated, and
10696
10696
// also transient so that we can just store data on it directly.
10697
- const result = createSymbol(symbol.flags, symbol.escapedName, CheckFlags.Instantiated | getCheckFlags(symbol) & (CheckFlags.Late | CheckFlags.OptionalParameter | CheckFlags.RestParameter));
10697
+ const result = createSymbol(symbol.flags, symbol.escapedName, CheckFlags.Instantiated | getCheckFlags(symbol) & (CheckFlags.Readonly | CheckFlags. Late | CheckFlags.OptionalParameter | CheckFlags.RestParameter));
10698
10698
result.declarations = symbol.declarations;
10699
10699
result.parent = symbol.parent;
10700
10700
result.target = symbol;
@@ -10825,11 +10825,11 @@ namespace ts {
10825
10825
return errorType;
10826
10826
}
10827
10827
type.instantiating = true;
10828
+ const modifiers = getMappedTypeModifiers(type);
10828
10829
const result = mapType(mappedTypeVariable, t => {
10829
10830
if (t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.InstantiableNonPrimitive | TypeFlags.Object | TypeFlags.Intersection) && t !== wildcardType) {
10830
10831
const replacementMapper = createReplacementMapper(typeVariable, t, mapper);
10831
- return isArrayType(t) ? createArrayType(instantiateMappedTypeTemplate(type, numberType, /*isOptional*/ true, replacementMapper)) :
10832
- isReadonlyArrayType(t) ? createReadonlyArrayType(instantiateMappedTypeTemplate(type, numberType, /*isOptional*/ true, replacementMapper)) :
10832
+ return isArrayType(t) ? createArrayType(instantiateMappedTypeTemplate(type, numberType, /*isOptional*/ true, replacementMapper), getModifiedReadonlyState(isReadonlyArrayType(t), modifiers)) :
10833
10833
isTupleType(t) ? instantiateMappedTupleType(t, type, replacementMapper) :
10834
10834
instantiateAnonymousType(type, replacementMapper);
10835
10835
}
@@ -10842,6 +10842,10 @@ namespace ts {
10842
10842
return instantiateAnonymousType(type, mapper);
10843
10843
}
10844
10844
10845
+ function getModifiedReadonlyState(state: boolean, modifiers: MappedTypeModifiers) {
10846
+ return modifiers & MappedTypeModifiers.IncludeReadonly ? true : modifiers & MappedTypeModifiers.ExcludeReadonly ? false : state;
10847
+ }
10848
+
10845
10849
function instantiateMappedTupleType(tupleType: TupleTypeReference, mappedType: MappedType, mapper: TypeMapper) {
10846
10850
const minLength = tupleType.target.minLength;
10847
10851
const elementTypes = map(tupleType.typeArguments || emptyArray, (_, i) =>
@@ -10850,7 +10854,8 @@ namespace ts {
10850
10854
const newMinLength = modifiers & MappedTypeModifiers.IncludeOptional ? 0 :
10851
10855
modifiers & MappedTypeModifiers.ExcludeOptional ? getTypeReferenceArity(tupleType) - (tupleType.target.hasRestElement ? 1 : 0) :
10852
10856
minLength;
10853
- return createTupleType(elementTypes, newMinLength, tupleType.target.hasRestElement, tupleType.target.associatedNames);
10857
+ const newReadonly = getModifiedReadonlyState(tupleType.target.readonly, modifiers);
10858
+ return createTupleType(elementTypes, newMinLength, tupleType.target.hasRestElement, newReadonly, tupleType.target.associatedNames);
10854
10859
}
10855
10860
10856
10861
function instantiateMappedTypeTemplate(type: MappedType, key: Type, isOptional: boolean, mapper: TypeMapper) {
@@ -12608,7 +12613,7 @@ namespace ts {
12608
12613
errorInfo = saveErrorInfo;
12609
12614
}
12610
12615
}
12611
- else if (isTupleType(source) && ( isArrayType(target) || isReadonlyArrayType(target) ) || isArrayType(source) && isReadonlyArrayType(target)) {
12616
+ else if (isTupleType(source) && isArrayType(target) || isArrayType(source) && isReadonlyArrayType(target)) {
12612
12617
return isRelatedTo(getIndexTypeOfType(source, IndexKind.Number) || anyType, getIndexTypeOfType(target, IndexKind.Number) || anyType, reportErrors);
12613
12618
}
12614
12619
// Even if relationship doesn't hold for unions, intersections, or generic type references,
@@ -13443,7 +13448,7 @@ namespace ts {
13443
13448
}
13444
13449
13445
13450
function isArrayType(type: Type): boolean {
13446
- return !!(getObjectFlags(type) & ObjectFlags.Reference) && (<TypeReference>type).target === globalArrayType;
13451
+ return !!(getObjectFlags(type) & ObjectFlags.Reference) && (( <TypeReference>type).target === globalArrayType || (<TypeReference>type).target === globalReadonlyArrayType) ;
13447
13452
}
13448
13453
13449
13454
function isReadonlyArrayType(type: Type): boolean {
@@ -13457,8 +13462,7 @@ namespace ts {
13457
13462
function isArrayLikeType(type: Type): boolean {
13458
13463
// A type is array-like if it is a reference to the global Array or global ReadonlyArray type,
13459
13464
// or if it is not the undefined or null type and if it is assignable to ReadonlyArray<any>
13460
- return getObjectFlags(type) & ObjectFlags.Reference && ((<TypeReference>type).target === globalArrayType || (<TypeReference>type).target === globalReadonlyArrayType) ||
13461
- !(type.flags & TypeFlags.Nullable) && isTypeAssignableTo(type, anyReadonlyArrayType);
13465
+ return isArrayType(type) || !(type.flags & TypeFlags.Nullable) && isTypeAssignableTo(type, anyReadonlyArrayType);
13462
13466
}
13463
13467
13464
13468
function isEmptyArrayLiteralType(type: Type): boolean {
@@ -14063,16 +14067,13 @@ namespace ts {
14063
14067
// For arrays and tuples we infer new arrays and tuples where the reverse mapping has been
14064
14068
// applied to the element type(s).
14065
14069
if (isArrayType(source)) {
14066
- return createArrayType(inferReverseMappedType((<TypeReference>source).typeArguments![0], target, constraint));
14067
- }
14068
- if (isReadonlyArrayType(source)) {
14069
- return createReadonlyArrayType(inferReverseMappedType((<TypeReference>source).typeArguments![0], target, constraint));
14070
+ return createArrayType(inferReverseMappedType((<TypeReference>source).typeArguments![0], target, constraint), isReadonlyArrayType(source));
14070
14071
}
14071
14072
if (isTupleType(source)) {
14072
14073
const elementTypes = map(source.typeArguments || emptyArray, t => inferReverseMappedType(t, target, constraint));
14073
14074
const minLength = getMappedTypeModifiers(target) & MappedTypeModifiers.IncludeOptional ?
14074
14075
getTypeReferenceArity(source) - (source.target.hasRestElement ? 1 : 0) : source.target.minLength;
14075
- return createTupleType(elementTypes, minLength, source.target.hasRestElement, source.target.associatedNames);
14076
+ return createTupleType(elementTypes, minLength, source.target.hasRestElement, source.target.readonly, source.target. associatedNames);
14076
14077
}
14077
14078
// For all other object types we infer a new object type where the reverse mapping has been
14078
14079
// applied to the type of each property.
@@ -17948,7 +17949,9 @@ namespace ts {
17948
17949
return createTupleType(elementTypes, minLength, hasRestElement);
17949
17950
}
17950
17951
}
17951
- return getArrayLiteralType(elementTypes, UnionReduction.Subtype);
17952
+ return createArrayType(elementTypes.length ?
17953
+ getUnionType(elementTypes, UnionReduction.Subtype) :
17954
+ strictNullChecks ? implicitNeverType : undefinedWideningType);
17952
17955
}
17953
17956
17954
17957
function getArrayLiteralTupleTypeIfApplicable(elementTypes: Type[], contextualType: Type | undefined, hasRestElement: boolean, elementCount = elementTypes.length) {
@@ -17977,12 +17980,6 @@ namespace ts {
17977
17980
}
17978
17981
}
17979
17982
17980
- function getArrayLiteralType(elementTypes: Type[], unionReduction = UnionReduction.Literal) {
17981
- return createArrayType(elementTypes.length ?
17982
- getUnionType(elementTypes, unionReduction) :
17983
- strictNullChecks ? implicitNeverType : undefinedWideningType);
17984
- }
17985
-
17986
17983
function isNumericName(name: DeclarationName): boolean {
17987
17984
switch (name.kind) {
17988
17985
case SyntaxKind.ComputedPropertyName:
@@ -21276,7 +21273,7 @@ namespace ts {
21276
21273
}
21277
21274
const minArgumentCount = getMinArgumentCount(source);
21278
21275
const minLength = minArgumentCount < start ? 0 : minArgumentCount - start;
21279
- return createTupleType(types, minLength, !!restType, names);
21276
+ return createTupleType(types, minLength, !!restType, /*readonly*/ false, names);
21280
21277
}
21281
21278
21282
21279
function getParameterCount(signature: Signature) {
0 commit comments