Skip to content

Allowed tuples to have both named and anonymous members #53356

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

Merged
merged 18 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Keep partial labels in createTupleTargetType
  • Loading branch information
Andarist committed Mar 27, 2023
commit bf4b8cb1ba7ad1f4979e0c1cff8d8f2a6db2ed6f
17 changes: 6 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15985,14 +15985,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return isTypeOperatorNode(node) && node.operator === SyntaxKind.ReadonlyKeyword;
}

function createTupleType(elementTypes: readonly Type[], elementFlags?: readonly ElementFlags[], readonly = false, namedMemberDeclarations?: readonly (NamedTupleMember | ParameterDeclaration | undefined)[]) {
function createTupleType(elementTypes: readonly Type[], elementFlags?: readonly ElementFlags[], readonly = false, namedMemberDeclarations: readonly (NamedTupleMember | ParameterDeclaration | undefined)[] = []) {
const tupleTarget = getTupleTargetType(elementFlags || map(elementTypes, _ => ElementFlags.Required), readonly, namedMemberDeclarations);
return tupleTarget === emptyGenericType ? emptyObjectType :
elementTypes.length ? createNormalizedTypeReference(tupleTarget, elementTypes) :
tupleTarget;
}

function getTupleTargetType(elementFlags: readonly ElementFlags[], readonly: boolean, namedMemberDeclarations?: readonly (NamedTupleMember | ParameterDeclaration | undefined)[]): GenericType {
function getTupleTargetType(elementFlags: readonly ElementFlags[], readonly: boolean, namedMemberDeclarations: readonly (NamedTupleMember | ParameterDeclaration | undefined)[]): GenericType {
if (elementFlags.length === 1 && elementFlags[0] & ElementFlags.Rest) {
// [...X[]] is equivalent to just X[]
return readonly ? globalReadonlyArrayType : globalArrayType;
Expand All @@ -16015,7 +16015,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
//
// Note that the generic type created by this function has no symbol associated with it. The same
// is true for each of the synthesized type parameters.
function createTupleTargetType(elementFlags: readonly ElementFlags[], readonly: boolean, namedMemberDeclarations: readonly (NamedTupleMember | ParameterDeclaration | undefined)[] | undefined): TupleType {
function createTupleTargetType(elementFlags: readonly ElementFlags[], readonly: boolean, namedMemberDeclarations: readonly (NamedTupleMember | ParameterDeclaration | undefined)[]): TupleType {
const arity = elementFlags.length;
const minLength = countWhere(elementFlags, f => !!(f & (ElementFlags.Required | ElementFlags.Variadic)));
let typeParameters: TypeParameter[] | undefined;
Expand Down Expand Up @@ -16097,7 +16097,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// In either layout, zero or more generic variadic elements may be present at any location.
const expandedTypes: Type[] = [];
const expandedFlags: ElementFlags[] = [];
let expandedDeclarations: (NamedTupleMember | ParameterDeclaration)[] | undefined = [];
const expandedDeclarations: (NamedTupleMember | ParameterDeclaration | undefined)[] = [];
let lastRequiredIndex = -1;
let firstRestIndex = -1;
let lastOptionalOrRestIndex = -1;
Expand Down Expand Up @@ -16140,7 +16140,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
(t, i) => expandedFlags[firstRestIndex + i] & ElementFlags.Variadic ? getIndexedAccessType(t, numberType) : t));
expandedTypes.splice(firstRestIndex + 1, lastOptionalOrRestIndex - firstRestIndex);
expandedFlags.splice(firstRestIndex + 1, lastOptionalOrRestIndex - firstRestIndex);
expandedDeclarations?.splice(firstRestIndex + 1, lastOptionalOrRestIndex - firstRestIndex);
expandedDeclarations.splice(firstRestIndex + 1, lastOptionalOrRestIndex - firstRestIndex);
}
const tupleTarget = getTupleTargetType(expandedFlags, target.readonly, expandedDeclarations);
return tupleTarget === emptyGenericType ? emptyObjectType :
Expand All @@ -16159,12 +16159,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
expandedTypes.push(flags & ElementFlags.Optional ? addOptionality(type, /*isProperty*/ true) : type);
expandedFlags.push(flags);
if (expandedDeclarations && declaration) {
expandedDeclarations.push(declaration);
}
else {
expandedDeclarations = undefined;
}
expandedDeclarations.push(declaration);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/genericRestParameters2.types
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ type T05<T extends any[]> = Parameters<(x: string, ...args: T) => void>;
>args : T

type T06 = T05<[number, ...boolean[]]>;
>T06 : [string, number, ...boolean[]]
>T06 : [x: string, number, ...boolean[]]

type P1<T extends Function> = T extends (head: infer A, ...tail: infer B) => any ? { head: A, tail: B } : any[];
>P1 : P1<T>
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/genericRestParameters3.types
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ ff2 = ff1;
function ff3<A extends unknown[]>(s1: (...args: [x: string, ...rest: A | [number]]) => void, s2: (x: string, ...rest: A | [number]) => void) {
>ff3 : <A extends unknown[]>(s1: (...args: [x: string, ...rest: A | [number]]) => void, s2: (x: string, ...rest: A | [number]) => void) => void
>s1 : (...args: [x: string, ...rest: A | [number]]) => void
>args : [string, number] | [x: string, ...rest: A]
>args : [x: string, number] | [x: string, ...rest: A]
>s2 : (x: string, ...rest: A | [number]) => void
>x : string
>rest : [number] | A

s1 = s2;
>s1 = s2 : (x: string, ...rest: [number] | A) => void
>s1 : (...args: [string, number] | [x: string, ...rest: A]) => void
>s1 : (...args: [x: string, number] | [x: string, ...rest: A]) => void
>s2 : (x: string, ...rest: [number] | A) => void

s2 = s1;
>s2 = s1 : (...args: [string, number] | [x: string, ...rest: A]) => void
>s2 = s1 : (...args: [x: string, number] | [x: string, ...rest: A]) => void
>s2 : (x: string, ...rest: [number] | A) => void
>s1 : (...args: [string, number] | [x: string, ...rest: A]) => void
>s1 : (...args: [x: string, number] | [x: string, ...rest: A]) => void
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type SubTup2VariadicAndRest<T extends unknown[]> = T extends [
: never;

type SubTup2VariadicAndRestTest = SubTup2VariadicAndRest<[a: 0, b: 1, ...c: 2[]]>;
>SubTup2VariadicAndRestTest : [0, 1, 2]
>SubTup2VariadicAndRestTest : [a: 0, b: 1, 2]

type SubTup2TrailingVariadic<T extends unknown[]> = T extends [
>SubTup2TrailingVariadic : SubTup2TrailingVariadic<T>
Expand All @@ -65,7 +65,7 @@ type SubTup2RestAndTrailingVariadic2<T extends unknown[]> = T extends [
: never;

type SubTup2RestAndTrailingVariadic2Test = SubTup2RestAndTrailingVariadic2<[...a: 0[], b: 1, c: 2]>;
>SubTup2RestAndTrailingVariadic2Test : [0, 1, 2]
>SubTup2RestAndTrailingVariadic2Test : [0, b: 1, c: 2]

type SubTup2VariadicWithLeadingFixedElements<T extends unknown[]> = T extends [
>SubTup2VariadicWithLeadingFixedElements : SubTup2VariadicWithLeadingFixedElements<T>
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/namedTupleMembersErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export type Segment2 = [number, size: number];
>Segment2 : [number, size: number]

export type List = [item: any, ...any];
>List : [any, ...any[]]
>List : [item: any, ...any[]]

export type List2 = [any, ...remainder: any];
>List2 : [any, ...any[]]
>List2 : [any, ...remainder: any[]]

export type Pair = [item: any, any?];
>Pair : [item: any, any?]
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/partiallyNamedTuples.types
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type ConditionalTuple = [
];

type AddMixedConditional<T> = [
>AddMixedConditional : [boolean, null, T extends number ? "a" : "b", ...T extends 0 ? [fourth: "c"] : []]
>AddMixedConditional : [first: boolean, null, third: T extends number ? "a" : "b", ...T extends 0 ? [fourth: "c"] : []]

first: boolean,
null,
Expand All @@ -78,11 +78,11 @@ type AddMixedConditional<T> = [
];

type AddMixedConditionalBoolean = AddMixedConditional<boolean>;
>AddMixedConditionalBoolean : [boolean, null, "b"]
>AddMixedConditionalBoolean : [first: boolean, null, third: "b"]

type AddMixedConditionalLiteral = AddMixedConditional<0>;
>AddMixedConditionalLiteral : [boolean, null, "a", "c"]
>AddMixedConditionalLiteral : [first: boolean, null, third: "a", fourth: "c"]

type AddMixedConditionalNumberPrimitive = AddMixedConditional<number>;
>AddMixedConditionalNumberPrimitive : [boolean, null, "a"]
>AddMixedConditionalNumberPrimitive : [first: boolean, null, third: "a"]

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy