Fixed tuple types indexed by a type parameter (#51037)

* Fixed tuple types indexed by a type parameter

* If only there was a tool to add that missing semicolon for me

* Rename to baseConstraint

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Josh Goldberg 2022-12-09 10:59:40 -05:00 committed by GitHub
parent 212d3864ef
commit f3d55db45a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 4 deletions

View File

@ -10090,8 +10090,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// If the parent is a tuple type, the rest element has a tuple type of the
// remaining tuple element types. Otherwise, the rest element has an array type with same
// element type as the parent type.
type = everyType(parentType, isTupleType) ?
mapType(parentType, t => sliceTupleType(t as TupleTypeReference, index)) :
const baseConstraint = getBaseConstraintOrType(parentType);
type = everyType(baseConstraint, isTupleType) ?
mapType(baseConstraint, t => sliceTupleType(t as TupleTypeReference, index)) :
createArrayType(elementType);
}
else if (isArrayLikeType(parentType)) {

View File

@ -132,7 +132,7 @@ function farr<T extends [number, string, string] | [string, number, number]>(x:
const [head, ...tail] = x;
>head : string | number
>tail : (string | number)[]
>tail : [string, string] | [number, number]
>x : [number, string, string] | [string, number, number]
if (x[0] === 'number') {
@ -144,7 +144,7 @@ function farr<T extends [number, string, string] | [string, number, number]>(x:
const [head, ...tail] = x;
>head : "number"
>tail : (string | number)[]
>tail : [string, string] | [number, number]
>x : [number, string, string] | [string, number, number]
}
}

View File

@ -0,0 +1,19 @@
//// [spreadTupleAccessedByTypeParameter.ts]
export function test<N extends number>(singletons: ["a"][], i: N) {
const singleton = singletons[i];
const [, ...rest] = singleton;
return rest;
}
//// [spreadTupleAccessedByTypeParameter.js]
"use strict";
exports.__esModule = true;
exports.test = void 0;
function test(singletons, i) {
var singleton = singletons[i];
var rest = singleton.slice(1);
return rest;
}
exports.test = test;

View File

@ -0,0 +1,21 @@
=== tests/cases/compiler/spreadTupleAccessedByTypeParameter.ts ===
export function test<N extends number>(singletons: ["a"][], i: N) {
>test : Symbol(test, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 0))
>N : Symbol(N, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 21))
>singletons : Symbol(singletons, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 39))
>i : Symbol(i, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 59))
>N : Symbol(N, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 21))
const singleton = singletons[i];
>singleton : Symbol(singleton, Decl(spreadTupleAccessedByTypeParameter.ts, 1, 7))
>singletons : Symbol(singletons, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 39))
>i : Symbol(i, Decl(spreadTupleAccessedByTypeParameter.ts, 0, 59))
const [, ...rest] = singleton;
>rest : Symbol(rest, Decl(spreadTupleAccessedByTypeParameter.ts, 2, 10))
>singleton : Symbol(singleton, Decl(spreadTupleAccessedByTypeParameter.ts, 1, 7))
return rest;
>rest : Symbol(rest, Decl(spreadTupleAccessedByTypeParameter.ts, 2, 10))
}

View File

@ -0,0 +1,21 @@
=== tests/cases/compiler/spreadTupleAccessedByTypeParameter.ts ===
export function test<N extends number>(singletons: ["a"][], i: N) {
>test : <N extends number>(singletons: ["a"][], i: N) => []
>singletons : ["a"][]
>i : N
const singleton = singletons[i];
>singleton : ["a"][][N]
>singletons[i] : ["a"][][N]
>singletons : ["a"][]
>i : N
const [, ...rest] = singleton;
> : undefined
>rest : []
>singleton : ["a"][][N]
return rest;
>rest : []
}

View File

@ -0,0 +1,6 @@
export function test<N extends number>(singletons: ["a"][], i: N) {
const singleton = singletons[i];
const [, ...rest] = singleton;
return rest;
}