Skip to content

Tail recursive evaluation of conditional types #45711

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 3 commits into from
Sep 8, 2021
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
Add tests
  • Loading branch information
ahejlsberg committed Sep 3, 2021
commit 093f76af3b71fe627724c898ac4189ecce89781d
49 changes: 49 additions & 0 deletions tests/baselines/reference/tailRecursiveConditionalTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tailRecursiveConditionalTypes.ts]
type Trim<S extends string> =
S extends ` ${infer T}` ? Trim<T> :
S extends `${infer T} ` ? Trim<T> :
S;

type T10 = Trim<' hello '>;
type T11 = Trim<' hello '>;

type GetChars<S> = GetCharsRec<S, never>;
type GetCharsRec<S, Acc> =
S extends `${infer Char}${infer Rest}` ? GetCharsRec<Rest, Char | Acc> : Acc;

type T20 = GetChars<'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'>;

type Reverse<T> = any[] extends T ? T : ReverseRec<T, []>;
type ReverseRec<T, Acc extends unknown[]> =
T extends [infer Head, ...infer Tail] ? ReverseRec<Tail, [Head, ...Acc]> : Acc;

type T30 = Reverse<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>;
type T31 = Reverse<string[]>;

type TupleOf<T, N extends number> = number extends N ? T[] : TupleOfRec<T, N, []>;
type TupleOfRec<T, N extends number, Acc extends unknown[]> =
Acc["length"] extends N ? Acc : TupleOfRec<T, N, [T, ...Acc]>;

type T40 = TupleOf<any, 200>;
type T41 = TupleOf<any, number>;


//// [tailRecursiveConditionalTypes.js]
"use strict";


//// [tailRecursiveConditionalTypes.d.ts]
declare type Trim<S extends string> = S extends ` ${infer T}` ? Trim<T> : S extends `${infer T} ` ? Trim<T> : S;
declare type T10 = Trim<' hello '>;
declare type T11 = Trim<' hello '>;
declare type GetChars<S> = GetCharsRec<S, never>;
declare type GetCharsRec<S, Acc> = S extends `${infer Char}${infer Rest}` ? GetCharsRec<Rest, Char | Acc> : Acc;
declare type T20 = GetChars<'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'>;
declare type Reverse<T> = any[] extends T ? T : ReverseRec<T, []>;
declare type ReverseRec<T, Acc extends unknown[]> = T extends [infer Head, ...infer Tail] ? ReverseRec<Tail, [Head, ...Acc]> : Acc;
declare type T30 = Reverse<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>;
declare type T31 = Reverse<string[]>;
declare type TupleOf<T, N extends number> = number extends N ? T[] : TupleOfRec<T, N, []>;
declare type TupleOfRec<T, N extends number, Acc extends unknown[]> = Acc["length"] extends N ? Acc : TupleOfRec<T, N, [T, ...Acc]>;
declare type T40 = TupleOf<any, 200>;
declare type T41 = TupleOf<any, number>;
118 changes: 118 additions & 0 deletions tests/baselines/reference/tailRecursiveConditionalTypes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
=== tests/cases/compiler/tailRecursiveConditionalTypes.ts ===
type Trim<S extends string> =
>Trim : Symbol(Trim, Decl(tailRecursiveConditionalTypes.ts, 0, 0))
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 0, 10))

S extends ` ${infer T}` ? Trim<T> :
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 0, 10))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 1, 23))
>Trim : Symbol(Trim, Decl(tailRecursiveConditionalTypes.ts, 0, 0))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 1, 23))

S extends `${infer T} ` ? Trim<T> :
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 0, 10))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 2, 22))
>Trim : Symbol(Trim, Decl(tailRecursiveConditionalTypes.ts, 0, 0))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 2, 22))

S;
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 0, 10))

type T10 = Trim<' hello '>;
>T10 : Symbol(T10, Decl(tailRecursiveConditionalTypes.ts, 3, 6))
>Trim : Symbol(Trim, Decl(tailRecursiveConditionalTypes.ts, 0, 0))

type T11 = Trim<' hello '>;
>T11 : Symbol(T11, Decl(tailRecursiveConditionalTypes.ts, 5, 170))
>Trim : Symbol(Trim, Decl(tailRecursiveConditionalTypes.ts, 0, 0))

type GetChars<S> = GetCharsRec<S, never>;
>GetChars : Symbol(GetChars, Decl(tailRecursiveConditionalTypes.ts, 6, 170))
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 8, 14))
>GetCharsRec : Symbol(GetCharsRec, Decl(tailRecursiveConditionalTypes.ts, 8, 41))
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 8, 14))

type GetCharsRec<S, Acc> =
>GetCharsRec : Symbol(GetCharsRec, Decl(tailRecursiveConditionalTypes.ts, 8, 41))
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 9, 17))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 9, 19))

S extends `${infer Char}${infer Rest}` ? GetCharsRec<Rest, Char | Acc> : Acc;
>S : Symbol(S, Decl(tailRecursiveConditionalTypes.ts, 9, 17))
>Char : Symbol(Char, Decl(tailRecursiveConditionalTypes.ts, 10, 22))
>Rest : Symbol(Rest, Decl(tailRecursiveConditionalTypes.ts, 10, 35))
>GetCharsRec : Symbol(GetCharsRec, Decl(tailRecursiveConditionalTypes.ts, 8, 41))
>Rest : Symbol(Rest, Decl(tailRecursiveConditionalTypes.ts, 10, 35))
>Char : Symbol(Char, Decl(tailRecursiveConditionalTypes.ts, 10, 22))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 9, 19))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 9, 19))

type T20 = GetChars<'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'>;
>T20 : Symbol(T20, Decl(tailRecursiveConditionalTypes.ts, 10, 81))
>GetChars : Symbol(GetChars, Decl(tailRecursiveConditionalTypes.ts, 6, 170))

type Reverse<T> = any[] extends T ? T : ReverseRec<T, []>;
>Reverse : Symbol(Reverse, Decl(tailRecursiveConditionalTypes.ts, 12, 86))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 14, 13))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 14, 13))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 14, 13))
>ReverseRec : Symbol(ReverseRec, Decl(tailRecursiveConditionalTypes.ts, 14, 58))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 14, 13))

type ReverseRec<T, Acc extends unknown[]> =
>ReverseRec : Symbol(ReverseRec, Decl(tailRecursiveConditionalTypes.ts, 14, 58))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 15, 16))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 15, 18))

T extends [infer Head, ...infer Tail] ? ReverseRec<Tail, [Head, ...Acc]> : Acc;
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 15, 16))
>Head : Symbol(Head, Decl(tailRecursiveConditionalTypes.ts, 16, 20))
>Tail : Symbol(Tail, Decl(tailRecursiveConditionalTypes.ts, 16, 35))
>ReverseRec : Symbol(ReverseRec, Decl(tailRecursiveConditionalTypes.ts, 14, 58))
>Tail : Symbol(Tail, Decl(tailRecursiveConditionalTypes.ts, 16, 35))
>Head : Symbol(Head, Decl(tailRecursiveConditionalTypes.ts, 16, 20))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 15, 18))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 15, 18))

type T30 = Reverse<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>;
>T30 : Symbol(T30, Decl(tailRecursiveConditionalTypes.ts, 16, 83))
>Reverse : Symbol(Reverse, Decl(tailRecursiveConditionalTypes.ts, 12, 86))

type T31 = Reverse<string[]>;
>T31 : Symbol(T31, Decl(tailRecursiveConditionalTypes.ts, 18, 171))
>Reverse : Symbol(Reverse, Decl(tailRecursiveConditionalTypes.ts, 12, 86))

type TupleOf<T, N extends number> = number extends N ? T[] : TupleOfRec<T, N, []>;
>TupleOf : Symbol(TupleOf, Decl(tailRecursiveConditionalTypes.ts, 19, 29))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 21, 13))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 21, 15))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 21, 15))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 21, 13))
>TupleOfRec : Symbol(TupleOfRec, Decl(tailRecursiveConditionalTypes.ts, 21, 82))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 21, 13))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 21, 15))

type TupleOfRec<T, N extends number, Acc extends unknown[]> =
>TupleOfRec : Symbol(TupleOfRec, Decl(tailRecursiveConditionalTypes.ts, 21, 82))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 22, 16))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 22, 18))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 22, 36))

Acc["length"] extends N ? Acc : TupleOfRec<T, N, [T, ...Acc]>;
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 22, 36))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 22, 18))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 22, 36))
>TupleOfRec : Symbol(TupleOfRec, Decl(tailRecursiveConditionalTypes.ts, 21, 82))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 22, 16))
>N : Symbol(N, Decl(tailRecursiveConditionalTypes.ts, 22, 18))
>T : Symbol(T, Decl(tailRecursiveConditionalTypes.ts, 22, 16))
>Acc : Symbol(Acc, Decl(tailRecursiveConditionalTypes.ts, 22, 36))

type T40 = TupleOf<any, 200>;
>T40 : Symbol(T40, Decl(tailRecursiveConditionalTypes.ts, 23, 66))
>TupleOf : Symbol(TupleOf, Decl(tailRecursiveConditionalTypes.ts, 19, 29))

type T41 = TupleOf<any, number>;
>T41 : Symbol(T41, Decl(tailRecursiveConditionalTypes.ts, 25, 29))
>TupleOf : Symbol(TupleOf, Decl(tailRecursiveConditionalTypes.ts, 19, 29))

53 changes: 53 additions & 0 deletions tests/baselines/reference/tailRecursiveConditionalTypes.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/tailRecursiveConditionalTypes.ts ===
type Trim<S extends string> =
>Trim : Trim<S>

S extends ` ${infer T}` ? Trim<T> :
S extends `${infer T} ` ? Trim<T> :
S;

type T10 = Trim<' hello '>;
>T10 : "hello"

type T11 = Trim<' hello '>;
>T11 : "hello"

type GetChars<S> = GetCharsRec<S, never>;
>GetChars : GetChars<S>

type GetCharsRec<S, Acc> =
>GetCharsRec : GetCharsRec<S, Acc>

S extends `${infer Char}${infer Rest}` ? GetCharsRec<Rest, Char | Acc> : Acc;

type T20 = GetChars<'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'>;
>T20 : "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

type Reverse<T> = any[] extends T ? T : ReverseRec<T, []>;
>Reverse : Reverse<T>

type ReverseRec<T, Acc extends unknown[]> =
>ReverseRec : ReverseRec<T, Acc>

T extends [infer Head, ...infer Tail] ? ReverseRec<Tail, [Head, ...Acc]> : Acc;

type T30 = Reverse<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>;
>T30 : [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

type T31 = Reverse<string[]>;
>T31 : string[]

type TupleOf<T, N extends number> = number extends N ? T[] : TupleOfRec<T, N, []>;
>TupleOf : TupleOf<T, N>

type TupleOfRec<T, N extends number, Acc extends unknown[]> =
>TupleOfRec : TupleOfRec<T, N, Acc>

Acc["length"] extends N ? Acc : TupleOfRec<T, N, [T, ...Acc]>;

type T40 = TupleOf<any, 200>;
>T40 : [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any]

type T41 = TupleOf<any, number>;
>T41 : any[]

30 changes: 30 additions & 0 deletions tests/cases/compiler/tailRecursiveConditionalTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @strict: true
// @declaration: true

type Trim<S extends string> =
S extends ` ${infer T}` ? Trim<T> :
S extends `${infer T} ` ? Trim<T> :
S;

type T10 = Trim<' hello '>;
type T11 = Trim<' hello '>;

type GetChars<S> = GetCharsRec<S, never>;
type GetCharsRec<S, Acc> =
S extends `${infer Char}${infer Rest}` ? GetCharsRec<Rest, Char | Acc> : Acc;

type T20 = GetChars<'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'>;

type Reverse<T> = any[] extends T ? T : ReverseRec<T, []>;
type ReverseRec<T, Acc extends unknown[]> =
T extends [infer Head, ...infer Tail] ? ReverseRec<Tail, [Head, ...Acc]> : Acc;

type T30 = Reverse<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>;
type T31 = Reverse<string[]>;

type TupleOf<T, N extends number> = number extends N ? T[] : TupleOfRec<T, N, []>;
type TupleOfRec<T, N extends number, Acc extends unknown[]> =
Acc["length"] extends N ? Acc : TupleOfRec<T, N, [T, ...Acc]>;

type T40 = TupleOf<any, 200>;
type T41 = TupleOf<any, number>;
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