-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve signature assignability for argument lists of different lengths #49218
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
Changes from 1 commit
1819397
67892c3
bd712ab
88027d5
6c5383d
46f2e57
0de8923
beb241d
388ec89
8753248
e357450
29979e2
fd52884
336288a
92d0267
1c24035
608f7c3
61d2612
919fc0e
b920e9a
a8ddfc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,9 @@ 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 '[y: string]'. | ||
| Type '[number, boolean]' is not assignable to type '[y: string]'. | ||
| Source has 2 element(s) but target allows only 1. | ||
| Type '[string] | [number]' is not assignable to type '[y: string]'. | ||
| Type '[number]' is not assignable to type '[y: string]'. | ||
| Type 'number' is not assignable to type 'string'. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To the best of my understanding, this change is correct and welcome under the implemented changes. |
||
| 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 '[y: number, z: boolean]'. | ||
|
|
@@ -69,9 +69,9 @@ 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 '[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. | ||
| !!! error TS2322: Type '[string] | [number]' is not assignable to type '[y: string]'. | ||
| !!! error TS2322: Type '[number]' is not assignable to type '[y: string]'. | ||
| !!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
|
Comment on lines
+73
to
+75
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a change of the error for this case: declare let f1: (x: string, ...args: [string] | [number, boolean]) => void;
declare let f2: (x: string, y: string) => void;
f1 = f2With the current changes in this PR, I create a slice from the target tuple and I cap it to the source's length~. I do that for similar reasons that I've outlined in the other comment. The source signature can freely ignore the "extra" arguments so we don't even need to check them here (we'd have to ignore them through some other mechanism anyway) |
||
| 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'. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| === tests/cases/compiler/restTupleUnionShorterContextualParams.ts === | ||
| // repro #48663 | ||
|
|
||
| // showcase how those transitive assignments are OK | ||
| const f1: (x: string | number) => void = x => {}; | ||
| >f1 : Symbol(f1, Decl(restTupleUnionShorterContextualParams.ts, 3, 5)) | ||
| >x : Symbol(x, Decl(restTupleUnionShorterContextualParams.ts, 3, 11)) | ||
| >x : Symbol(x, Decl(restTupleUnionShorterContextualParams.ts, 3, 40)) | ||
|
|
||
| const f2: (x: string | number, y: string | number) => void = f1; | ||
| >f2 : Symbol(f2, Decl(restTupleUnionShorterContextualParams.ts, 4, 5)) | ||
| >x : Symbol(x, Decl(restTupleUnionShorterContextualParams.ts, 4, 11)) | ||
| >y : Symbol(y, Decl(restTupleUnionShorterContextualParams.ts, 4, 30)) | ||
| >f1 : Symbol(f1, Decl(restTupleUnionShorterContextualParams.ts, 3, 5)) | ||
|
|
||
| const f3: (...args: [number, string] | [string, number]) => void = f2; | ||
| >f3 : Symbol(f3, Decl(restTupleUnionShorterContextualParams.ts, 5, 5)) | ||
| >args : Symbol(args, Decl(restTupleUnionShorterContextualParams.ts, 5, 11)) | ||
| >f2 : Symbol(f2, Decl(restTupleUnionShorterContextualParams.ts, 4, 5)) | ||
|
|
||
| // by extension those should be OK too | ||
| const f4: (...args: [number, string] | [string, number]) => void = (item) => {} | ||
| >f4 : Symbol(f4, Decl(restTupleUnionShorterContextualParams.ts, 8, 5)) | ||
| >args : Symbol(args, Decl(restTupleUnionShorterContextualParams.ts, 8, 11)) | ||
| >item : Symbol(item, Decl(restTupleUnionShorterContextualParams.ts, 8, 68)) | ||
|
|
||
| const f5: (...args: [number, string] | [string, number]) => void = (item: number | string) => {} | ||
| >f5 : Symbol(f5, Decl(restTupleUnionShorterContextualParams.ts, 9, 5)) | ||
| >args : Symbol(args, Decl(restTupleUnionShorterContextualParams.ts, 9, 11)) | ||
| >item : Symbol(item, Decl(restTupleUnionShorterContextualParams.ts, 9, 68)) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| === tests/cases/compiler/restTupleUnionShorterContextualParams.ts === | ||
| // repro #48663 | ||
|
|
||
| // showcase how those transitive assignments are OK | ||
| const f1: (x: string | number) => void = x => {}; | ||
| >f1 : (x: string | number) => void | ||
| >x : string | number | ||
| >x => {} : (x: string | number) => void | ||
| >x : string | number | ||
|
|
||
| const f2: (x: string | number, y: string | number) => void = f1; | ||
| >f2 : (x: string | number, y: string | number) => void | ||
| >x : string | number | ||
| >y : string | number | ||
| >f1 : (x: string | number) => void | ||
|
|
||
| const f3: (...args: [number, string] | [string, number]) => void = f2; | ||
| >f3 : (...args: [number, string] | [string, number]) => void | ||
| >args : [number, string] | [string, number] | ||
| >f2 : (x: string | number, y: string | number) => void | ||
|
|
||
| // by extension those should be OK too | ||
| const f4: (...args: [number, string] | [string, number]) => void = (item) => {} | ||
| >f4 : (...args: [number, string] | [string, number]) => void | ||
| >args : [number, string] | [string, number] | ||
| >(item) => {} : (item: string | number) => void | ||
| >item : string | number | ||
|
|
||
| const f5: (...args: [number, string] | [string, number]) => void = (item: number | string) => {} | ||
| >f5 : (...args: [number, string] | [string, number]) => void | ||
| >args : [number, string] | [string, number] | ||
| >(item: number | string) => {} : (item: number | string) => void | ||
| >item : string | number | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // @noEmit: true | ||
|
|
||
| // repro #48663 | ||
|
|
||
| // showcase how those transitive assignments are OK | ||
| const f1: (x: string | number) => void = x => {}; | ||
| const f2: (x: string | number, y: string | number) => void = f1; | ||
| const f3: (...args: [number, string] | [string, number]) => void = f2; | ||
|
|
||
| // by extension those should be OK too | ||
| const f4: (...args: [number, string] | [string, number]) => void = (item) => {} | ||
| const f5: (...args: [number, string] | [string, number]) => void = (item: number | string) => {} |
Uh oh!
There was an error while loading. Please reload this page.