This commit is contained in:
Jake Bailey 2023-03-23 12:52:05 -07:00 committed by GitHub
parent c66f8de7c8
commit 9bd1a3225b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 130 additions and 24 deletions

View File

@ -19875,7 +19875,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, /*readonly*/ true) : tryGetTypeAtPosition(source, i);
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : 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
@ -34724,12 +34724,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}
function getRestTypeAtPosition(source: Signature, pos: number, readonly = false): Type {
function getRestTypeAtPosition(source: Signature, pos: number): 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), readonly);
return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType));
}
const types = [];
const flags = [];
@ -34748,7 +34748,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
names.push(name);
}
}
return createTupleType(types, flags, readonly, length(names) === length(types) ? names : undefined);
return createTupleType(types, flags, /*readonly*/ false, length(names) === length(types) ? names : undefined);
}
// Return the number of parameters in a signature. The rest parameter, if present, counts as one

View File

@ -0,0 +1,13 @@
//// [classImplementsMethodWIthTupleArgs.ts]
declare class MySettable implements Settable {
set(option: Record<string, unknown>): void;
set(name: string, value: unknown): void;
}
interface Settable {
set(...args: [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]): void;
}
//// [classImplementsMethodWIthTupleArgs.js]
"use strict";

View File

@ -0,0 +1,25 @@
=== tests/cases/compiler/classImplementsMethodWIthTupleArgs.ts ===
declare class MySettable implements Settable {
>MySettable : Symbol(MySettable, Decl(classImplementsMethodWIthTupleArgs.ts, 0, 0))
>Settable : Symbol(Settable, Decl(classImplementsMethodWIthTupleArgs.ts, 3, 1))
set(option: Record<string, unknown>): void;
>set : Symbol(MySettable.set, Decl(classImplementsMethodWIthTupleArgs.ts, 0, 46), Decl(classImplementsMethodWIthTupleArgs.ts, 1, 47))
>option : Symbol(option, Decl(classImplementsMethodWIthTupleArgs.ts, 1, 8))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
set(name: string, value: unknown): void;
>set : Symbol(MySettable.set, Decl(classImplementsMethodWIthTupleArgs.ts, 0, 46), Decl(classImplementsMethodWIthTupleArgs.ts, 1, 47))
>name : Symbol(name, Decl(classImplementsMethodWIthTupleArgs.ts, 2, 8))
>value : Symbol(value, Decl(classImplementsMethodWIthTupleArgs.ts, 2, 21))
}
interface Settable {
>Settable : Symbol(Settable, Decl(classImplementsMethodWIthTupleArgs.ts, 3, 1))
set(...args: [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]): void;
>set : Symbol(Settable.set, Decl(classImplementsMethodWIthTupleArgs.ts, 5, 20))
>args : Symbol(args, Decl(classImplementsMethodWIthTupleArgs.ts, 6, 8))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
}

View File

@ -0,0 +1,20 @@
=== tests/cases/compiler/classImplementsMethodWIthTupleArgs.ts ===
declare class MySettable implements Settable {
>MySettable : MySettable
set(option: Record<string, unknown>): void;
>set : { (option: Record<string, unknown>): void; (name: string, value: unknown): void; }
>option : Record<string, unknown>
set(name: string, value: unknown): void;
>set : { (option: Record<string, unknown>): void; (name: string, value: unknown): void; }
>name : string
>value : unknown
}
interface Settable {
set(...args: [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]): void;
>set : (...args: [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]) => void
>args : [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]
}

View File

@ -0,0 +1,33 @@
tests/cases/compiler/contextualTupleTypeParameterReadonly.ts(10,8): error TS2345: Argument of type '(a: 1 | 2, b: "1" | "2") => void' is not assignable to parameter of type '(...args: readonly [1, "1"] | readonly [2, "2"]) => any'.
Types of parameters 'a' and 'args' are incompatible.
Type 'readonly [1, "1"] | readonly [2, "2"]' is not assignable to type '[a: 1 | 2, b: "1" | "2"]'.
The type 'readonly [1, "1"]' is 'readonly' and cannot be assigned to the mutable type '[a: 1 | 2, b: "1" | "2"]'.
==== tests/cases/compiler/contextualTupleTypeParameterReadonly.ts (1 errors) ====
declare function each<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (fn: (...args: T) => any) => void;
const cases = [
[1, '1'],
[2, '2'],
] as const;
const eacher = each(cases);
eacher((a, b) => {
~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: 1 | 2, b: "1" | "2") => void' is not assignable to parameter of type '(...args: readonly [1, "1"] | readonly [2, "2"]) => any'.
!!! error TS2345: Types of parameters 'a' and 'args' are incompatible.
!!! error TS2345: Type 'readonly [1, "1"] | readonly [2, "2"]' is not assignable to type '[a: 1 | 2, b: "1" | "2"]'.
!!! error TS2345: The type 'readonly [1, "1"]' is 'readonly' and cannot be assigned to the mutable type '[a: 1 | 2, b: "1" | "2"]'.
a;
b;
});
// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
const [a, b] = args;
a;
b;
});

View File

@ -13,6 +13,7 @@ eacher((a, b) => {
b;
});
// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
const [a, b] = args;
a;
@ -31,6 +32,7 @@ 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++) {

View File

@ -36,20 +36,21 @@ 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, 14, 8))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 15, 8))
const [a, b] = args;
>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 : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 16, 11))
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 16, 13))
>args : Symbol(args, Decl(contextualTupleTypeParameterReadonly.ts, 15, 8))
a;
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 15, 11))
>a : Symbol(a, Decl(contextualTupleTypeParameterReadonly.ts, 16, 11))
b;
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 15, 13))
>b : Symbol(b, Decl(contextualTupleTypeParameterReadonly.ts, 16, 13))
});

View File

@ -43,6 +43,7 @@ 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

View File

@ -6,19 +6,19 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(18,1): error TS2345
Source has 0 element(s) but target requires 2.
tests/cases/conformance/types/rest/genericRestParameters3.ts(23,1): error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
Types of parameters 'y' and 'args' are incompatible.
Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: string]'.
Type '[number, boolean]' is not assignable to type 'readonly [y: string]'.
Type '[string] | [number, boolean]' is not assignable to type '[y: string]'.
Type '[number, boolean]' is not assignable to type '[y: string]'.
Source has 2 element(s) but target allows only 1.
tests/cases/conformance/types/rest/genericRestParameters3.ts(24,1): error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
Types of parameters 'y' and 'args' are incompatible.
Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: number, z: boolean]'.
Type '[string]' is not assignable to type 'readonly [y: number, z: boolean]'.
Type '[string] | [number, boolean]' is not assignable to type '[y: number, z: boolean]'.
Type '[string]' is not assignable to type '[y: number, z: boolean]'.
Source has 1 element(s) but target requires 2.
tests/cases/conformance/types/rest/genericRestParameters3.ts(35,1): error TS2554: Expected 1 arguments, but got 0.
tests/cases/conformance/types/rest/genericRestParameters3.ts(36,21): error TS2345: Argument of type 'number' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(37,21): error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
Types of parameters 'cb' and 'args' are incompatible.
Property '0' is missing in type 'CoolArray<any>' but required in type 'readonly [cb: (...args: any[]) => void]'.
Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(44,32): error TS2345: Argument of type '[10, 20]' is not assignable to parameter of type 'CoolArray<number>'.
Property 'hello' is missing in type '[10, 20]' but required in type 'CoolArray<number>'.
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
@ -69,15 +69,15 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(59,5): error TS2345
~~
!!! error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: string]'.
!!! error TS2322: Type '[number, boolean]' is not assignable to type 'readonly [y: string]'.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[y: string]'.
!!! error TS2322: Type '[number, boolean]' is not assignable to type '[y: string]'.
!!! error TS2322: Source has 2 element(s) but target allows only 1.
f1 = f3; // Error
~~
!!! error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: number, z: boolean]'.
!!! error TS2322: Type '[string]' is not assignable to type 'readonly [y: number, z: boolean]'.
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[y: number, z: boolean]'.
!!! error TS2322: Type '[string]' is not assignable to type '[y: number, z: boolean]'.
!!! error TS2322: Source has 1 element(s) but target requires 2.
f1 = f4;
@ -100,7 +100,7 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(59,5): error TS2345
~~~
!!! error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
!!! error TS2345: Types of parameters 'cb' and 'args' are incompatible.
!!! error TS2345: Property '0' is missing in type 'CoolArray<any>' but required in type 'readonly [cb: (...args: any[]) => void]'.
!!! error TS2345: Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
function bar<T extends any[]>(...args: T): T {
return args;

View File

@ -1,7 +1,7 @@
tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
Types of parameters 'b' and 'args' are incompatible.
Type 'T' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
Type 'any[]' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
Type 'T' is not assignable to type '[b: T[0], ...x: T[number][]]'.
Type 'any[]' is not assignable to type '[b: T[0], ...x: T[number][]]'.
Source provides no match for required element at position 0 in target.
@ -65,8 +65,8 @@ tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error
~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
!!! error TS2345: Types of parameters 'b' and 'args' are incompatible.
!!! error TS2345: Type 'T' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
!!! error TS2345: Type 'any[]' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
!!! error TS2345: Type 'T' is not assignable to type '[b: T[0], ...x: T[number][]]'.
!!! error TS2345: Type 'any[]' is not assignable to type '[b: T[0], ...x: T[number][]]'.
!!! error TS2345: Source provides no match for required element at position 0 in target.
}

View File

@ -0,0 +1,10 @@
// @strict: true
declare class MySettable implements Settable {
set(option: Record<string, unknown>): void;
set(name: string, value: unknown): void;
}
interface Settable {
set(...args: [option: Record<string, unknown>] | [name: string, value: unknown] | [name: string]): void;
}

View File

@ -14,6 +14,7 @@ eacher((a, b) => {
b;
});
// TODO: https://github.com/microsoft/TypeScript/issues/53255
eacher((...args) => {
const [a, b] = args;
a;