Ensure rest type for source parameter is readonly in relations (#53258)

This commit is contained in:
Jake Bailey
2023-03-20 16:18:52 -07:00
committed by GitHub
parent f43a4fe401
commit e9836a4bec
7 changed files with 200 additions and 18 deletions

View File

@@ -19850,7 +19850,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const restIndex = sourceRestType || targetRestType ? paramCount - 1 : -1;
for (let i = 0; i < paramCount; i++) {
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : tryGetTypeAtPosition(source, i);
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i, /*readonly*/ true) : tryGetTypeAtPosition(source, i);
const targetType = i === restIndex ? getRestTypeAtPosition(target, i) : tryGetTypeAtPosition(target, i);
if (sourceType && targetType) {
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
@@ -34656,12 +34656,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}
function getRestTypeAtPosition(source: Signature, pos: number): Type {
function getRestTypeAtPosition(source: Signature, pos: number, readonly = false): Type {
const parameterCount = getParameterCount(source);
const minArgumentCount = getMinArgumentCount(source);
const restType = getEffectiveRestType(source);
if (restType && pos >= parameterCount - 1) {
return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType));
return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType), readonly);
}
const types = [];
const flags = [];
@@ -34680,7 +34680,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
names.push(name);
}
}
return createTupleType(types, flags, /*readonly*/ false, length(names) === length(types) ? names : undefined);
return createTupleType(types, flags, readonly, length(names) === length(types) ? names : undefined);
}
// Return the number of parameters in a signature. The rest parameter, if present, counts as one