• 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/compiler/controlFlowForCatchAndFinally.ts ===
20declare function AssertType(value:any, type:string):void;
21type Page = {close(): Promise<void>; content(): Promise<string>};
22type Browser = {close(): Promise<void>};
23declare function test1(): Promise<Browser>;
24declare function test2(obj: Browser): Promise<Page>;
25async function test(): Promise<string> {
26    let browser: Browser | undefined = undefined;
27AssertType(browser, "union");
28AssertType(undefined, "undefined");
29
30    let page: Page | undefined = undefined;
31AssertType(page, "union");
32AssertType(undefined, "undefined");
33
34    try {
35        browser = await test1();
36AssertType(browser = await test1(), "Browser");
37AssertType(browser, "union");
38AssertType(await test1(), "Browser");
39AssertType(test1(), "Promise<Browser>");
40AssertType(test1, "() => Promise<Browser>");
41
42        page = await test2(browser);
43AssertType(page = await test2(browser), "Page");
44AssertType(page, "union");
45AssertType(await test2(browser), "Page");
46AssertType(test2(browser), "Promise<Page>");
47AssertType(test2, "(Browser) => Promise<Page>");
48AssertType(browser, "Browser");
49
50AssertType(await page.content(), "string");
51AssertType(page.content(), "Promise<string>");
52AssertType(page.content, "() => Promise<string>");
53        return await page.content();;
54
55    } finally {
56        if (page) {
57AssertType(page, "union");
58
59            await page.close(); // ok
60AssertType(await page.close(), "void");
61AssertType(page.close(), "Promise<void>");
62AssertType(page.close, "() => Promise<void>");
63        }
64
65        if (browser) {
66AssertType(browser, "union");
67
68            await browser.close(); // ok
69AssertType(await browser.close(), "void");
70AssertType(browser.close(), "Promise<void>");
71AssertType(browser.close, "() => Promise<void>");
72        }
73    }
74}
75
76declare class Aborter { abort(): void };
77class Foo {
78    abortController: Aborter | undefined = undefined;
79
80    async operation() {
81        if (this.abortController !== undefined) {
82AssertType(this.abortController !== undefined, "boolean");
83AssertType(this.abortController, "union");
84AssertType(this, "this");
85AssertType(undefined, "undefined");
86
87            this.abortController.abort();
88AssertType(this.abortController.abort(), "void");
89AssertType(this.abortController.abort, "() => void");
90AssertType(this.abortController, "Aborter");
91AssertType(this, "this");
92
93            this.abortController = undefined;
94AssertType(this.abortController = undefined, "undefined");
95AssertType(this.abortController, "union");
96AssertType(this, "this");
97AssertType(undefined, "undefined");
98        }
99        try {
100            this.abortController = new Aborter();
101AssertType(this.abortController = new Aborter(), "Aborter");
102AssertType(this.abortController, "union");
103AssertType(this, "this");
104AssertType(new Aborter(), "Aborter");
105AssertType(Aborter, "typeof Aborter");
106
107        } catch (error) {
108AssertType(error, "unknown");
109
110            if (this.abortController !== undefined) {
111AssertType(this.abortController !== undefined, "boolean");
112AssertType(this.abortController, "union");
113AssertType(this, "this");
114AssertType(undefined, "undefined");
115
116                this.abortController.abort(); // ok
117AssertType(this.abortController.abort(), "void");
118AssertType(this.abortController.abort, "() => void");
119AssertType(this.abortController, "Aborter");
120AssertType(this, "this");
121            }
122        }
123    }
124}
125
126