Skip to content

Always compare rest types as mutable #55793

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20377,8 +20377,8 @@ 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 targetType = i === restIndex ? getRestTypeAtPosition(target, i) : tryGetTypeAtPosition(target, i);
const sourceType = i === restIndex ? getMutableArrayOrTupleType(getRestTypeAtPosition(source, i), /*forceVariadic*/ false) : tryGetTypeAtPosition(source, i);
const targetType = i === restIndex ? getMutableArrayOrTupleType(getRestTypeAtPosition(target, i), /*forceVariadic*/ false) : 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
// how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
Expand Down Expand Up @@ -33195,11 +33195,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getInferredTypes(context);
}

function getMutableArrayOrTupleType(type: Type) {
return type.flags & TypeFlags.Union ? mapType(type, getMutableArrayOrTupleType) :
// TODO(jakebailey): cache?
function getMutableArrayOrTupleType(type: Type, forceVariadic: boolean): Type {
return type.flags & TypeFlags.Union ? mapType(type, t => getMutableArrayOrTupleType(t, forceVariadic)) :
type.flags & TypeFlags.Any || isMutableArrayOrTuple(getBaseConstraintOfType(type) || type) ? type :
isTupleType(type) ? createTupleType(getElementTypes(type), type.target.elementFlags, /*readonly*/ false, type.target.labeledElementDeclarations) :
createTupleType([type], [ElementFlags.Variadic]);
forceVariadic ? createTupleType([type], [ElementFlags.Variadic]) : type;
}

function getSpreadArgumentType(args: readonly Expression[], index: number, argCount: number, restType: Type, context: InferenceContext | undefined, checkMode: CheckMode) {
Expand All @@ -33214,7 +33215,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
checkExpressionWithContextualType((arg as SpreadElement).expression, restType, context, checkMode);

if (isArrayLikeType(spreadType)) {
return getMutableArrayOrTupleType(spreadType);
return getMutableArrayOrTupleType(spreadType, /*forceVariadic*/ true);
}

return createArrayType(checkIteratedTypeOrElementType(IterationUse.Spread, spreadType, undefinedType, arg.kind === SyntaxKind.SpreadElement ? (arg as SpreadElement).expression : arg), inConstContext);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ eacher((a, b) => {
b;
});

// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
const [a, b] = args;
a;
Expand All @@ -34,7 +33,6 @@ eacher(function (a, b) {
a;
b;
});
// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher(function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ eacher((a, b) => {

});

// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
>eacher : Symbol(eacher, Decl(contextualTupleTypeParameterReadonly.ts, 7, 5))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 15, 8))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 14, 8))

const [a, b] = args;
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 16, 11))
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 16, 13))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 15, 8))
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 15, 11))
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 15, 13))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 14, 8))

a;
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 16, 11))
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 15, 11))

b;
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 16, 13))
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 15, 13))

});

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ eacher((a, b) => {

});

// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
>eacher((...args) => { const [a, b] = args; a; b;}) : void
>eacher : (fn: (...args: readonly [1, "1"] | readonly [2, "2"]) => any) => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ eacher((a, b) => {
b;
});

// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
const [a, b] = args;
a;
Expand Down