Skip to content

Commit 3f1ee89

Browse files
committed
tweak code, add tests
1 parent 1784567 commit 3f1ee89

File tree

8 files changed

+709
-7
lines changed

8 files changed

+709
-7
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28933,15 +28933,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2893328933
const signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl as FunctionExpression);
2893428934
if (signature && !isResolvingReturnTypeOfSignature(signature)) {
2893528935
const returnType = getReturnTypeOfSignature(signature);
28936-
if (returnType.flags & (TypeFlags.Any | TypeFlags.Void)) {
28937-
return returnType;
28938-
}
2893928936
const functionFlags = getFunctionFlags(functionDecl);
2894028937
if (functionFlags & FunctionFlags.Generator) {
28941-
return filterType(returnType, t => checkGeneratorInstantiationAssignabilityToReturnType(t, functionFlags, /*errorNode*/ undefined));
28938+
return filterType(returnType, t => {
28939+
return !!(t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void | TypeFlags.InstantiableNonPrimitive)) || checkGeneratorInstantiationAssignabilityToReturnType(t, functionFlags, /*errorNode*/ undefined);
28940+
});
2894228941
}
2894328942
if (functionFlags & FunctionFlags.Async) {
28944-
return filterType(returnType, t => !!getAwaitedTypeOfPromise(t));
28943+
return filterType(returnType, t => {
28944+
return !!(t.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void | TypeFlags.InstantiableNonPrimitive)) || !!getAwaitedTypeOfPromise(t);
28945+
});
2894528946
}
2894628947
return returnType;
2894728948
}

tests/baselines/reference/contextuallyTypeAsyncFunctionReturnType.symbols

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,182 @@ async function fn4(): Promise<Obj> {
5252

5353
});
5454
}
55+
56+
declare class Context {
57+
>Context : Symbol(Context, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 20, 1))
58+
59+
private _runnable;
60+
>_runnable : Symbol(Context._runnable, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 22, 23))
61+
}
62+
type Done = (err?: any) => void;
63+
>Done : Symbol(Done, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 24, 1))
64+
>err : Symbol(err, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 25, 13))
65+
66+
type Func = (this: Context, done: Done) => void;
67+
>Func : Symbol(Func, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 25, 32))
68+
>this : Symbol(this, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 26, 13))
69+
>Context : Symbol(Context, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 20, 1))
70+
>done : Symbol(done, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 26, 27))
71+
>Done : Symbol(Done, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 24, 1))
72+
73+
type AsyncFunc = (this: Context) => PromiseLike<any>;
74+
>AsyncFunc : Symbol(AsyncFunc, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 26, 48))
75+
>this : Symbol(this, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 27, 18))
76+
>Context : Symbol(Context, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 20, 1))
77+
>PromiseLike : Symbol(PromiseLike, Decl(lib.es5.d.ts, --, --))
78+
79+
interface TestFunction {
80+
>TestFunction : Symbol(TestFunction, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 27, 53))
81+
82+
(fn: Func): void;
83+
>fn : Symbol(fn, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 30, 3))
84+
>Func : Symbol(Func, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 25, 32))
85+
86+
(fn: AsyncFunc): void;
87+
>fn : Symbol(fn, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 31, 3))
88+
>AsyncFunc : Symbol(AsyncFunc, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 26, 48))
89+
90+
(title: string, fn?: Func): void;
91+
>title : Symbol(title, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 32, 3))
92+
>fn : Symbol(fn, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 32, 17))
93+
>Func : Symbol(Func, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 25, 32))
94+
95+
(title: string, fn?: AsyncFunc): void;
96+
>title : Symbol(title, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 33, 3))
97+
>fn : Symbol(fn, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 33, 17))
98+
>AsyncFunc : Symbol(AsyncFunc, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 26, 48))
99+
}
100+
101+
declare const test: TestFunction;
102+
>test : Symbol(test, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 36, 13))
103+
>TestFunction : Symbol(TestFunction, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 27, 53))
104+
105+
interface ProcessTreeNode {
106+
>ProcessTreeNode : Symbol(ProcessTreeNode, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 36, 33))
107+
108+
pid: number;
109+
>pid : Symbol(ProcessTreeNode.pid, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 38, 27))
110+
111+
name: string;
112+
>name : Symbol(ProcessTreeNode.name, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 39, 14))
113+
114+
memory?: number;
115+
>memory : Symbol(ProcessTreeNode.memory, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 40, 15))
116+
117+
commandLine?: string;
118+
>commandLine : Symbol(ProcessTreeNode.commandLine, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 41, 18))
119+
120+
children: ProcessTreeNode[];
121+
>children : Symbol(ProcessTreeNode.children, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 42, 23))
122+
>ProcessTreeNode : Symbol(ProcessTreeNode, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 36, 33))
123+
}
124+
125+
export declare function getProcessTree(
126+
>getProcessTree : Symbol(getProcessTree, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 44, 1))
127+
128+
rootPid: number,
129+
>rootPid : Symbol(rootPid, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 46, 39))
130+
131+
callback: (tree: ProcessTreeNode) => void
132+
>callback : Symbol(callback, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 47, 18))
133+
>tree : Symbol(tree, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 48, 13))
134+
>ProcessTreeNode : Symbol(ProcessTreeNode, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 36, 33))
135+
136+
): void;
137+
138+
test("windows-process-tree", async () => {
139+
>test : Symbol(test, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 36, 13))
140+
141+
return new Promise((resolve, reject) => {
142+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
143+
>resolve : Symbol(resolve, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 52, 22))
144+
>reject : Symbol(reject, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 52, 30))
145+
146+
getProcessTree(123, (tree) => {
147+
>getProcessTree : Symbol(getProcessTree, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 44, 1))
148+
>tree : Symbol(tree, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 53, 25))
149+
150+
if (tree) {
151+
>tree : Symbol(tree, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 53, 25))
152+
153+
resolve();
154+
>resolve : Symbol(resolve, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 52, 22))
155+
156+
} else {
157+
reject(new Error("windows-process-tree"));
158+
>reject : Symbol(reject, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 52, 30))
159+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --))
160+
}
161+
});
162+
});
163+
});
164+
165+
interface ILocalExtension {
166+
>ILocalExtension : Symbol(ILocalExtension, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 61, 3))
167+
168+
isApplicationScoped: boolean;
169+
>isApplicationScoped : Symbol(ILocalExtension.isApplicationScoped, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 63, 27))
170+
171+
publisherId: string | null;
172+
>publisherId : Symbol(ILocalExtension.publisherId, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 64, 31))
173+
}
174+
type Metadata = {
175+
>Metadata : Symbol(Metadata, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 66, 1))
176+
177+
updated: boolean;
178+
>updated : Symbol(updated, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 67, 17))
179+
180+
};
181+
declare function scanMetadata(
182+
>scanMetadata : Symbol(scanMetadata, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 69, 2))
183+
184+
local: ILocalExtension
185+
>local : Symbol(local, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 70, 30))
186+
>ILocalExtension : Symbol(ILocalExtension, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 61, 3))
187+
188+
): Promise<Metadata | undefined>;
189+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
190+
>Metadata : Symbol(Metadata, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 66, 1))
191+
192+
async function copyExtensions(
193+
>copyExtensions : Symbol(copyExtensions, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 72, 33))
194+
195+
fromExtensions: ILocalExtension[]
196+
>fromExtensions : Symbol(fromExtensions, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 74, 30))
197+
>ILocalExtension : Symbol(ILocalExtension, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 61, 3))
198+
199+
): Promise<void> {
200+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
201+
202+
const extensions: [ILocalExtension, Metadata | undefined][] =
203+
>extensions : Symbol(extensions, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 77, 7))
204+
>ILocalExtension : Symbol(ILocalExtension, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 61, 3))
205+
>Metadata : Symbol(Metadata, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 66, 1))
206+
207+
await Promise.all(
208+
>Promise.all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
209+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
210+
>all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
211+
212+
fromExtensions
213+
>fromExtensions .filter((e) => !e.isApplicationScoped) .map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
214+
>fromExtensions .filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
215+
>fromExtensions : Symbol(fromExtensions, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 74, 30))
216+
217+
.filter((e) => !e.isApplicationScoped)
218+
>filter : Symbol(Array.filter, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
219+
>e : Symbol(e, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 80, 17))
220+
>e.isApplicationScoped : Symbol(ILocalExtension.isApplicationScoped, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 63, 27))
221+
>e : Symbol(e, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 80, 17))
222+
>isApplicationScoped : Symbol(ILocalExtension.isApplicationScoped, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 63, 27))
223+
224+
.map(async (e) => [e, await scanMetadata(e)])
225+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
226+
>e : Symbol(e, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 81, 20))
227+
>e : Symbol(e, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 81, 20))
228+
>scanMetadata : Symbol(scanMetadata, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 69, 2))
229+
>e : Symbol(e, Decl(contextuallyTypeAsyncFunctionReturnType.ts, 81, 20))
230+
231+
);
232+
}
233+

tests/baselines/reference/contextuallyTypeAsyncFunctionReturnType.types

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,179 @@ async function fn4(): Promise<Obj> {
5959

6060
});
6161
}
62+
63+
declare class Context {
64+
>Context : Context
65+
66+
private _runnable;
67+
>_runnable : any
68+
}
69+
type Done = (err?: any) => void;
70+
>Done : (err?: any) => void
71+
>err : any
72+
73+
type Func = (this: Context, done: Done) => void;
74+
>Func : (this: Context, done: Done) => void
75+
>this : Context
76+
>done : Done
77+
78+
type AsyncFunc = (this: Context) => PromiseLike<any>;
79+
>AsyncFunc : (this: Context) => PromiseLike<any>
80+
>this : Context
81+
82+
interface TestFunction {
83+
(fn: Func): void;
84+
>fn : Func
85+
86+
(fn: AsyncFunc): void;
87+
>fn : AsyncFunc
88+
89+
(title: string, fn?: Func): void;
90+
>title : string
91+
>fn : Func
92+
93+
(title: string, fn?: AsyncFunc): void;
94+
>title : string
95+
>fn : AsyncFunc
96+
}
97+
98+
declare const test: TestFunction;
99+
>test : TestFunction
100+
101+
interface ProcessTreeNode {
102+
pid: number;
103+
>pid : number
104+
105+
name: string;
106+
>name : string
107+
108+
memory?: number;
109+
>memory : number
110+
111+
commandLine?: string;
112+
>commandLine : string
113+
114+
children: ProcessTreeNode[];
115+
>children : ProcessTreeNode[]
116+
}
117+
118+
export declare function getProcessTree(
119+
>getProcessTree : (rootPid: number, callback: (tree: ProcessTreeNode) => void) => void
120+
121+
rootPid: number,
122+
>rootPid : number
123+
124+
callback: (tree: ProcessTreeNode) => void
125+
>callback : (tree: ProcessTreeNode) => void
126+
>tree : ProcessTreeNode
127+
128+
): void;
129+
130+
test("windows-process-tree", async () => {
131+
>test("windows-process-tree", async () => { return new Promise((resolve, reject) => { getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }); });}) : void
132+
>test : TestFunction
133+
>"windows-process-tree" : "windows-process-tree"
134+
>async () => { return new Promise((resolve, reject) => { getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }); });} : () => Promise<void>
135+
136+
return new Promise((resolve, reject) => {
137+
>new Promise((resolve, reject) => { getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }); }) : Promise<void>
138+
>Promise : PromiseConstructor
139+
>(resolve, reject) => { getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }); } : (resolve: (value: void | PromiseLike<void>) => void, reject: (reason?: any) => void) => void
140+
>resolve : (value: void | PromiseLike<void>) => void
141+
>reject : (reason?: any) => void
142+
143+
getProcessTree(123, (tree) => {
144+
>getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }) : void
145+
>getProcessTree : (rootPid: number, callback: (tree: ProcessTreeNode) => void) => void
146+
>123 : 123
147+
>(tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } } : (tree: ProcessTreeNode) => void
148+
>tree : ProcessTreeNode
149+
150+
if (tree) {
151+
>tree : ProcessTreeNode
152+
153+
resolve();
154+
>resolve() : void
155+
>resolve : (value: void | PromiseLike<void>) => void
156+
157+
} else {
158+
reject(new Error("windows-process-tree"));
159+
>reject(new Error("windows-process-tree")) : void
160+
>reject : (reason?: any) => void
161+
>new Error("windows-process-tree") : Error
162+
>Error : ErrorConstructor
163+
>"windows-process-tree" : "windows-process-tree"
164+
}
165+
});
166+
});
167+
});
168+
169+
interface ILocalExtension {
170+
isApplicationScoped: boolean;
171+
>isApplicationScoped : boolean
172+
173+
publisherId: string | null;
174+
>publisherId : string
175+
}
176+
type Metadata = {
177+
>Metadata : { updated: boolean; }
178+
179+
updated: boolean;
180+
>updated : boolean
181+
182+
};
183+
declare function scanMetadata(
184+
>scanMetadata : (local: ILocalExtension) => Promise<Metadata | undefined>
185+
186+
local: ILocalExtension
187+
>local : ILocalExtension
188+
189+
): Promise<Metadata | undefined>;
190+
191+
async function copyExtensions(
192+
>copyExtensions : (fromExtensions: ILocalExtension[]) => Promise<void>
193+
194+
fromExtensions: ILocalExtension[]
195+
>fromExtensions : ILocalExtension[]
196+
197+
): Promise<void> {
198+
const extensions: [ILocalExtension, Metadata | undefined][] =
199+
>extensions : [ILocalExtension, Metadata][]
200+
201+
await Promise.all(
202+
>await Promise.all( fromExtensions .filter((e) => !e.isApplicationScoped) .map(async (e) => [e, await scanMetadata(e)]) ) : [ILocalExtension, Metadata][]
203+
>Promise.all( fromExtensions .filter((e) => !e.isApplicationScoped) .map(async (e) => [e, await scanMetadata(e)]) ) : Promise<[ILocalExtension, Metadata][]>
204+
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
205+
>Promise : PromiseConstructor
206+
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
207+
208+
fromExtensions
209+
>fromExtensions .filter((e) => !e.isApplicationScoped) .map(async (e) => [e, await scanMetadata(e)]) : Promise<[ILocalExtension, Metadata]>[]
210+
>fromExtensions .filter((e) => !e.isApplicationScoped) .map : <U>(callbackfn: (value: ILocalExtension, index: number, array: ILocalExtension[]) => U, thisArg?: any) => U[]
211+
>fromExtensions .filter((e) => !e.isApplicationScoped) : ILocalExtension[]
212+
>fromExtensions .filter : { <S extends ILocalExtension>(predicate: (value: ILocalExtension, index: number, array: ILocalExtension[]) => value is S, thisArg?: any): S[]; (predicate: (value: ILocalExtension, index: number, array: ILocalExtension[]) => unknown, thisArg?: any): ILocalExtension[]; }
213+
>fromExtensions : ILocalExtension[]
214+
215+
.filter((e) => !e.isApplicationScoped)
216+
>filter : { <S extends ILocalExtension>(predicate: (value: ILocalExtension, index: number, array: ILocalExtension[]) => value is S, thisArg?: any): S[]; (predicate: (value: ILocalExtension, index: number, array: ILocalExtension[]) => unknown, thisArg?: any): ILocalExtension[]; }
217+
>(e) => !e.isApplicationScoped : (e: ILocalExtension) => boolean
218+
>e : ILocalExtension
219+
>!e.isApplicationScoped : boolean
220+
>e.isApplicationScoped : boolean
221+
>e : ILocalExtension
222+
>isApplicationScoped : boolean
223+
224+
.map(async (e) => [e, await scanMetadata(e)])
225+
>map : <U>(callbackfn: (value: ILocalExtension, index: number, array: ILocalExtension[]) => U, thisArg?: any) => U[]
226+
>async (e) => [e, await scanMetadata(e)] : (e: ILocalExtension) => Promise<[ILocalExtension, Metadata]>
227+
>e : ILocalExtension
228+
>[e, await scanMetadata(e)] : [ILocalExtension, Metadata]
229+
>e : ILocalExtension
230+
>await scanMetadata(e) : Metadata
231+
>scanMetadata(e) : Promise<Metadata>
232+
>scanMetadata : (local: ILocalExtension) => Promise<Metadata>
233+
>e : ILocalExtension
234+
235+
);
236+
}
237+

0 commit comments

Comments
 (0)