• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Copyright (c) 2023 Huawei Device Co., Ltd.
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*
16* This file has been modified by Huawei to verify type inference by adding verification statements.
17*/
18
19// === tests/cases/conformance/types/mapped/mappedTypesArraysTuples.ts ===
20declare function AssertType(value:any, type:string):void;
21type Box<T> = { value: T };
22type Boxified<T> = { [P in keyof T]: Box<T[P]> };
23
24type T00 = Boxified<[number, string?, ...boolean[]]>;
25type T01 = Partial<[number, string?, ...boolean[]]>;
26type T02 = Required<[number, string?, ...boolean[]]>;
27
28type T10 = Boxified<string[]>;
29type T11 = Partial<string[]>;
30type T12 = Required<string[]>;
31type T13 = Boxified<ReadonlyArray<string>>;
32type T14 = Partial<ReadonlyArray<string>>;
33type T15 = Required<ReadonlyArray<string>>;
34
35type T20 = Boxified<(string | undefined)[]>;
36type T21 = Partial<(string | undefined)[]>;
37type T22 = Required<(string | undefined)[]>;
38type T23 = Boxified<ReadonlyArray<string | undefined>>;
39type T24 = Partial<ReadonlyArray<string | undefined>>;
40type T25 = Required<ReadonlyArray<string | undefined>>;
41
42type T30 = Boxified<Partial<string[]>>;
43type T31 = Partial<Boxified<string[]>>;
44
45type A = { a: string };
46type B = { b: string };
47
48type T40 = Boxified<A | A[] | ReadonlyArray<A> | [A, B] | string | string[]>;
49
50type ReadWrite<T> = { -readonly [P in keyof T] : T[P] };
51
52type T50 = Readonly<string[]>;
53type T51 = Readonly<[number, number]>;
54type T52 = Partial<Readonly<string[]>>;
55type T53 = Readonly<Partial<string[]>>;
56type T54 = ReadWrite<Required<T53>>;
57
58declare function unboxify<T>(x: Boxified<T>): T;
59
60declare let x10: [Box<number>, Box<string>, ...Box<boolean>[]];
61AssertType(x10, "[Box<number>, Box<string>, ...Box<boolean>[]]");
62
63let y10 = unboxify(x10);
64AssertType(y10, "[number, string, ...boolean[]]");
65AssertType(unboxify(x10), "[number, string, ...boolean[]]");
66AssertType(unboxify, "<T>(Boxified<T>) => T");
67AssertType(x10, "[Box<number>, Box<string>, ...Box<boolean>[]]");
68
69declare let x11: Box<number>[];
70AssertType(x11, "Box<number>[]");
71
72let y11 = unboxify(x11);
73AssertType(y11, "number[]");
74AssertType(unboxify(x11), "number[]");
75AssertType(unboxify, "<T>(Boxified<T>) => T");
76AssertType(x11, "Box<number>[]");
77
78declare let x12: { a: Box<number>, b: Box<string[]> };
79AssertType(x12, "{ a: Box<number>; b: Box<string[]>; }");
80AssertType(a, "Box<number>");
81AssertType(b, "Box<string[]>");
82
83let y12 = unboxify(x12);
84AssertType(y12, "{ a: number; b: string[]; }");
85AssertType(unboxify(x12), "{ a: number; b: string[]; }");
86AssertType(unboxify, "<T>(Boxified<T>) => T");
87AssertType(x12, "{ a: Box<number>; b: Box<string[]>; }");
88
89declare function nonpartial<T>(x: Partial<T>): T;
90
91declare let x20: [number | undefined, string?, ...boolean[]];
92AssertType(x20, "[union, (union)?, ...boolean[]]");
93
94let y20 = nonpartial(x20);
95AssertType(y20, "[number, string, ...boolean[]]");
96AssertType(nonpartial(x20), "[number, string, ...boolean[]]");
97AssertType(nonpartial, "<T>(Partial<T>) => T");
98AssertType(x20, "[union, (union)?, ...boolean[]]");
99
100declare let x21: (number | undefined)[];
101AssertType(x21, "(union)[]");
102
103let y21 = nonpartial(x21);
104AssertType(y21, "number[]");
105AssertType(nonpartial(x21), "number[]");
106AssertType(nonpartial, "<T>(Partial<T>) => T");
107AssertType(x21, "(union)[]");
108
109declare let x22: { a: number | undefined, b?: string[] };
110AssertType(x22, "{ a: union; b?: union; }");
111AssertType(a, "union");
112AssertType(b, "union");
113
114let y22 = nonpartial(x22);
115AssertType(y22, "{ a: number; b: string[]; }");
116AssertType(nonpartial(x22), "{ a: number; b: string[]; }");
117AssertType(nonpartial, "<T>(Partial<T>) => T");
118AssertType(x22, "{ a: union; b?: union; }");
119
120type __Awaited<T> = T extends PromiseLike<infer U> ? U : T;
121type Awaitified<T> = { [P in keyof T]: __Awaited<T[P]> };
122
123declare function all<T extends any[]>(...values: T): Promise<Awaitified<T>>;
124
125function f1(a: number, b: Promise<number>, c: string[], d: Promise<string[]>) {
126    let x1 = all(a);
127AssertType(x1, "Promise<[number]>");
128AssertType(all(a), "Promise<[number]>");
129AssertType(all, "<T extends any[]>(...T) => Promise<Awaitified<T>>");
130AssertType(a, "number");
131
132    let x2 = all(a, b);
133AssertType(x2, "Promise<[number, number]>");
134AssertType(all(a, b), "Promise<[number, number]>");
135AssertType(all, "<T extends any[]>(...T) => Promise<Awaitified<T>>");
136AssertType(a, "number");
137AssertType(b, "Promise<number>");
138
139    let x3 = all(a, b, c);
140AssertType(x3, "Promise<[number, number, string[]]>");
141AssertType(all(a, b, c), "Promise<[number, number, string[]]>");
142AssertType(all, "<T extends any[]>(...T) => Promise<Awaitified<T>>");
143AssertType(a, "number");
144AssertType(b, "Promise<number>");
145AssertType(c, "string[]");
146
147    let x4 = all(a, b, c, d);
148AssertType(x4, "Promise<[number, number, string[], string[]]>");
149AssertType(all(a, b, c, d), "Promise<[number, number, string[], string[]]>");
150AssertType(all, "<T extends any[]>(...T) => Promise<Awaitified<T>>");
151AssertType(a, "number");
152AssertType(b, "Promise<number>");
153AssertType(c, "string[]");
154AssertType(d, "Promise<string[]>");
155}
156
157function f2<T extends any[]>(a: Boxified<T>) {
158    let x: Box<any> | undefined = a.pop();
159AssertType(x, "union");
160AssertType(a.pop(), "union");
161AssertType(a.pop, "() => union");
162
163    let y: Box<any>[] = a.concat(a);
164AssertType(y, "Box<any>[]");
165AssertType(a.concat(a), "Box<any>[]");
166AssertType(a.concat, "{ (...ConcatArray<Box<any>>[]): Box<any>[]; (...(union)[]): Box<any>[]; }");
167AssertType(a, "Boxified<T>");
168}
169
170// Repro from #26163
171
172type ElementType<T> = T extends Array<infer U> ? U : never;
173type Mapped<T> = { [K in keyof T]: T[K] };
174
175type F<T> = ElementType<Mapped<T>>;
176type R1 = F<[string, number, boolean]>;  // string | number | boolean
177type R2 = ElementType<Mapped<[string, number, boolean]>>;  // string | number | boolean
178
179// Repro from #26163
180
181declare function acceptArray(arr: any[]): void;
182declare function mapArray<T extends any[]>(arr: T): Mapped<T>;
183function acceptMappedArray<T extends any[]>(arr: T) {
184    acceptArray(mapArray(arr));
185AssertType(acceptArray(mapArray(arr)), "void");
186AssertType(acceptArray, "(any[]) => void");
187AssertType(mapArray(arr), "Mapped<T>");
188AssertType(mapArray, "<T extends any[]>(T) => Mapped<T>");
189AssertType(arr, "T");
190}
191
192// Repro from #26163
193
194type Unconstrained<T> = ElementType<Mapped<T>>;
195type T1 = Unconstrained<[string, number, boolean]>;  // string | number | boolean
196
197type Constrained<T extends any[]> = ElementType<Mapped<T>>;
198type T2 = Constrained<[string, number, boolean]>;  // string | number | boolean
199
200
201