Uh oh!
There was an error while loading. Please reload this page.
Proper treatment of splicing tuples in array literals - #36861
Conversation
672b0de to
ebea675CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Nathan Shively-Sanders (sandersn)
left a comment
There was a problem hiding this comment.
Looks basically right and the baselines improve. I agree with Wesley Wigham (@weswigham) that isTupleLikeType is preferable.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ebea675 to
f17ba81Compare
Nathan Shively-Sanders (sandersn)
left a comment
There was a problem hiding this comment.
I think it's a good idea to have the isTupleLike implementation in the PR history but we always Squash & Merge so it should end up as one commit on master anyway.
Fixesmicrosoft#32465. After this was done, I continued to extend the implementation to handle TupleLike types but it'ss broken since JS doesn't allow splicing TupleLike values into array literals so pulled that out, and instead keeping it for reference below. (It Includes tests, which are broken too.) modified src/compiler/checker.ts @@ -22268,6 +22268,21 @@ namespace ts { else hasNonEndingSpreadElement = true; } } + else if (spreadType && isTupleLikeType(spreadType)) { + let i = 0, tupleEltType: Type | undefined; + while (tupleEltType = getTypeOfPropertyOfType(spreadType, "" + i as __String)) { + elementTypes.push(tupleEltType); + i++; + } + const stringIndexInfo = getIndexInfoOfType(spreadType, IndexKind.String); + const numberIndexInfo = getIndexInfoOfType(spreadType, IndexKind.Number); + if (stringIndexInfo || numberIndexInfo) { + if (stringIndexInfo) elementTypes.push(stringIndexInfo.type); + if (numberIndexInfo) elementTypes.push(numberIndexInfo.type); + if (i === elementCount - 1) hasEndingSpreadElement = true; + else hasNonEndingSpreadElement = true; + } + } else { if (inDestructuringPattern && spreadType) { // Given the following situation: new file tests/cases/compiler/spliceTupleLikesWIntegers.ts @@ -0,0 +1,23 @@ +declare const sb: { [0]: string, [1]: boolean }; + +let k1: [number, string, boolean]; +k1 = [1, ...sb]; + +let k2: [number, string, boolean, number]; +k2 = [1, ...sb, 1]; + +// declare const sb_: [string, ...boolean[]]; + +// let k3: [number, string, ...boolean[]]; +// k3 = [1, ...sb_]; + +// declare const sbb_: [string, boolean, ...boolean[]]; + +// let k4: [number, string, ...boolean[]]; +// k4 = [1, ...sbb_]; + +// let k5: [number, string, boolean, ...boolean[]]; +// k5 = [1, ...sbb_]; + +// let k6: [number, string, boolean, boolean, ...boolean[]]; +// k6 = [1, ...sbb_]; new file tests/cases/compiler/spliceTupleLikesWStrings.ts @@ -0,0 +1,23 @@ +declare const sb: { 0: string, 1: boolean }; + +let k1: [number, string, boolean]; +k1 = [1, ...sb]; + +let k2: [number, string, boolean, number]; +k2 = [1, ...sb, 1]; + +declare const sb_: { 0: string, [s: string]: (boolean|string) }; + +let k3: [number, string, ...(boolean|string)[]]; +k3 = [1, ...sb_]; + +declare const sbb_: { 0: string, 1: boolean, [s: string]: (boolean|string) }; + +let k4: [number, string, boolean, ...(boolean|string)[]]; +k4 = [1, ...sbb_]; + +// let k5: [number, string, boolean, ...(boolean|string)[]]; +// k5 = [1, ...sbb_]; + +// let k6: [number, string, boolean, boolean, ...(boolean|string)[]]; +// k6 = [1, ...sbb_];
f17ba81 to
db1c440CompareThank you for the fix. Would it solve this issue as well? constdate: Date=newDate();constnum=42;// OKconst[a,b]: [Date,number]=[date,num];constargs=[date,num];// Type '(number | Date)[]' is missing the following properties from type '[Date, number]': 0, 1(2739)const[c,d]: [Date,number]=args; |
No, that's caused by the lack of contextual typing in |
WeiSheng_ (WeiShengv99)
commented
May 18, 2020
i dont know what happend in this case , is this the same problem? |
Eli Barzilay (elibarzilay)
commented
May 18, 2020
@GRJser, no -- this was only about splicing into array literals. There is this PR that fixes some spreads in calls and might be relevant for your example. |
Fixes#32465.