• 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/statements/throwStatements/throwInEnclosingStatements.ts ===
20declare function AssertType(value:any, type:string):void;
21function fn(x) {
22    throw x;
23AssertType(x, "any");
24}
25
26<T>(x: T) => { throw x;
27AssertType(<T>(x: T) => { throw x; }, "<T>(T) => never");
28
29AssertType(x, "T");
30
31AssertType(x, "T");
32}
33
34let y: string;
35AssertType(y, "string");
36
37switch (y) {
38    case 'a':
39        throw y;
40    default:
41        throw y;
42}
43
44let z = 0;
45AssertType(z, "number");
46AssertType(0, "int");
47
48while (z < 10) {
49    throw z;
50AssertType(z, "number");
51}
52
53for (let i = 0; ;) { throw i;
54AssertType(i, "number");
55}
56
57for (let idx in {}) { throw idx;
58AssertType(idx, "string");
59}
60
61do { throw null; }while(true)
62AssertType(null, "null");
63
64let j = 0;
65AssertType(j, "number");
66AssertType(0, "int");
67
68while (j < 0) { throw j;
69AssertType(j, "number");
70}
71
72class C<T> {
73    private value: T;
74    biz() {
75        throw this.value;
76AssertType(this.value, "T");
77AssertType(this, "this");
78    }
79
80    constructor() {
81        throw this;
82AssertType(this, "this");
83    }
84}
85
86let aa = {
87AssertType(aa, "{ id: number; biz(): never; }");
88AssertType({    id:12,    biz() {        throw this;    }}, "{ id: number; biz(): never; }");
89
90    id:12,
91AssertType(id, "number");
92AssertType(12, "int");
93
94    biz() {
95AssertType(biz, "() => never");
96
97        throw this;
98AssertType(this, "any");
99    }
100}
101
102
103