Skip to content

Commit 60dd512

Browse files
authored
fix(60223): add Promise.try() to ESNext lib (#60232)
1 parent 80eeb4e commit 60dd512

File tree

79 files changed

+1525
-740
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1525
-740
lines changed

src/compiler/commandLineParser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ const libEntries: [string, string][] = [
247247
["esnext.regexp", "lib.es2024.regexp.d.ts"],
248248
["esnext.string", "lib.es2024.string.d.ts"],
249249
["esnext.iterator", "lib.esnext.iterator.d.ts"],
250+
["esnext.promise", "lib.esnext.promise.d.ts"],
250251
["decorators", "lib.decorators.d.ts"],
251252
["decorators.legacy", "lib.decorators.legacy.d.ts"],
252253
];

src/lib/esnext.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/// <reference lib="esnext.collection" />
66
/// <reference lib="esnext.array" />
77
/// <reference lib="esnext.iterator" />
8+
/// <reference lib="esnext.promise" />

src/lib/esnext.promise.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface PromiseConstructor {
2+
/**
3+
* Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result
4+
* in a Promise.
5+
*
6+
* @param callbackFn A function that is called synchronously. It can do anything: either return
7+
* a value, throw an error, or return a promise.
8+
* @param args Additional arguments, that will be passed to the callback.
9+
*
10+
* @returns A Promise that is:
11+
* - Already fulfilled, if the callback synchronously returns a value.
12+
* - Already rejected, if the callback synchronously throws an error.
13+
* - Asynchronously fulfilled or rejected, if the callback returns a promise.
14+
*/
15+
try<T, U extends unknown[]>(callbackFn: (...args: U) => T | PromiseLike<T>, ...args: U): Promise<Awaited<T>>;
16+
}

src/lib/libs.json

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"esnext.collection",
8686
"esnext.array",
8787
"esnext.iterator",
88+
"esnext.promise",
8889
"decorators",
8990
"decorators.legacy",
9091
// Default libraries

tests/baselines/reference/bundlerDirectoryModule(moduleresolution=bundler).trace.json

+13
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,19 @@
10061006
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
10071007
"Directory '/node_modules' does not exist, skipping all lookups in it.",
10081008
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
1009+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
1010+
"Explicitly specified module resolution kind: 'Node10'.",
1011+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
1012+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
1013+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1014+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1015+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1016+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1017+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
1018+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
1019+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1020+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1021+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
10091022
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
10101023
"Explicitly specified module resolution kind: 'Node10'.",
10111024
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/bundlerDirectoryModule(moduleresolution=nodenext).trace.json

+15
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,21 @@
11641164
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
11651165
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
11661166
"File '/package.json' does not exist according to earlier cached lookups.",
1167+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
1168+
"Explicitly specified module resolution kind: 'Node10'.",
1169+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
1170+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
1171+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1172+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1173+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1174+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1175+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
1176+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
1177+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1178+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1179+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
1180+
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
1181+
"File '/package.json' does not exist according to earlier cached lookups.",
11671182
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
11681183
"Explicitly specified module resolution kind: 'Node10'.",
11691184
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/modulePreserve2.trace.json

+12
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,18 @@
948948
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
949949
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
950950
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
951+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
952+
"Explicitly specified module resolution kind: 'Node10'.",
953+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
954+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
955+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
956+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
957+
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
958+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
959+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
960+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
961+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
962+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
951963
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
952964
"Explicitly specified module resolution kind: 'Node10'.",
953965
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/modulePreserve3.trace.json

+11
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,17 @@
875875
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
876876
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
877877
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
878+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
879+
"Explicitly specified module resolution kind: 'Node10'.",
880+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
881+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
882+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
883+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
884+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
885+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
886+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
887+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
888+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
878889
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
879890
"Explicitly specified module resolution kind: 'Node10'.",
880891
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/nodeModulesExportsBlocksTypesVersions(module=nodenext).trace.json

+14
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,20 @@
12731273
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
12741274
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
12751275
"File '/package.json' does not exist according to earlier cached lookups.",
1276+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
1277+
"Explicitly specified module resolution kind: 'Node10'.",
1278+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
1279+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
1280+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1281+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1282+
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
1283+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1284+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
1285+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
1286+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1287+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
1288+
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
1289+
"File '/package.json' does not exist according to earlier cached lookups.",
12761290
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
12771291
"Explicitly specified module resolution kind: 'Node10'.",
12781292
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/nodeModulesPackageImports(module=nodenext).trace.json

+15
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,21 @@
11991199
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
12001200
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
12011201
"File '/package.json' does not exist according to earlier cached lookups.",
1202+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
1203+
"Explicitly specified module resolution kind: 'Node10'.",
1204+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
1205+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
1206+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1207+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1208+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1209+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1210+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
1211+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
1212+
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
1213+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1214+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
1215+
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
1216+
"File '/package.json' does not exist according to earlier cached lookups.",
12021217
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
12031218
"Explicitly specified module resolution kind: 'Node10'.",
12041219
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/nodeModulesPackagePatternExportsTrailers(module=nodenext).trace.json

+14
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,20 @@
12481248
"======== Module name '@typescript/lib-esnext/iterator' was not resolved. ========",
12491249
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
12501250
"File '/package.json' does not exist according to earlier cached lookups.",
1251+
"======== Resolving module '@typescript/lib-esnext/promise' from '/.src/__lib_node_modules_lookup_lib.esnext.promise.d.ts__.ts'. ========",
1252+
"Explicitly specified module resolution kind: 'Node10'.",
1253+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: TypeScript, Declaration.",
1254+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
1255+
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
1256+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1257+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1258+
"Scoped package detected, looking in 'typescript__lib-esnext/promise'",
1259+
"Loading module '@typescript/lib-esnext/promise' from 'node_modules' folder, target file types: JavaScript.",
1260+
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
1261+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1262+
"======== Module name '@typescript/lib-esnext/promise' was not resolved. ========",
1263+
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
1264+
"File '/package.json' does not exist according to earlier cached lookups.",
12511265
"======== Resolving module '@typescript/lib-dom' from '/.src/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
12521266
"Explicitly specified module resolution kind: 'Node10'.",
12531267
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//// [tests/cases/compiler/promiseTry.ts] ////
2+
3+
//// [promiseTry.ts]
4+
Promise.try(() => {
5+
return "Sync result";
6+
});
7+
8+
Promise.try(async () => {
9+
return "Async result";
10+
});
11+
12+
const a = Promise.try(() => "Sync result");
13+
const b = Promise.try(async () => "Async result");
14+
15+
// SINGLE PARAMETER
16+
Promise.try((foo: string) => "Async result", "foo");
17+
Promise.try((foo) => "Async result", "foo");
18+
// @ts-expect-error too few parameters
19+
Promise.try((foo) => "Async result");
20+
Promise.try((foo: string | undefined) => "Async result", undefined);
21+
Promise.try((foo: string | undefined) => "Async result", "foo");
22+
Promise.try((foo) => "Async result", undefined);
23+
// @ts-expect-error too many parameters
24+
Promise.try(() => "Async result", "foo");
25+
26+
// MULTIPLE PARAMETERS
27+
Promise.try((foo: string, bar: number) => "Async result", "foo", 42);
28+
// @ts-expect-error too many parameters
29+
Promise.try((foo: string, bar: number) => "Async result", "foo", 42, "baz");
30+
// @ts-expect-error too few parameters
31+
Promise.try((foo: string, bar: number) => "Async result", "foo");
32+
Promise.try((foo: string, bar?: number) => "Async result", "foo");
33+
Promise.try((foo: string, bar?: number) => "Async result", "foo", undefined);
34+
Promise.try((foo: string, bar?: number) => "Async result", "foo", 42);
35+
36+
37+
//// [promiseTry.js]
38+
Promise.try(() => {
39+
return "Sync result";
40+
});
41+
Promise.try(async () => {
42+
return "Async result";
43+
});
44+
const a = Promise.try(() => "Sync result");
45+
const b = Promise.try(async () => "Async result");
46+
// SINGLE PARAMETER
47+
Promise.try((foo) => "Async result", "foo");
48+
Promise.try((foo) => "Async result", "foo");
49+
// @ts-expect-error too few parameters
50+
Promise.try((foo) => "Async result");
51+
Promise.try((foo) => "Async result", undefined);
52+
Promise.try((foo) => "Async result", "foo");
53+
Promise.try((foo) => "Async result", undefined);
54+
// @ts-expect-error too many parameters
55+
Promise.try(() => "Async result", "foo");
56+
// MULTIPLE PARAMETERS
57+
Promise.try((foo, bar) => "Async result", "foo", 42);
58+
// @ts-expect-error too many parameters
59+
Promise.try((foo, bar) => "Async result", "foo", 42, "baz");
60+
// @ts-expect-error too few parameters
61+
Promise.try((foo, bar) => "Async result", "foo");
62+
Promise.try((foo, bar) => "Async result", "foo");
63+
Promise.try((foo, bar) => "Async result", "foo", undefined);
64+
Promise.try((foo, bar) => "Async result", "foo", 42);

0 commit comments

Comments
 (0)