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/classes/members/instanceAndStaticMembers/typeOfThisInStaticMembers.ts === 20declare function AssertType(value:any, type:string):void; 21class C { 22 constructor(x: number) { } 23 static foo: number; 24 static bar() { 25 // type of this is the constructor function type 26 let t = this; 27AssertType(t, "typeof C"); 28AssertType(this, "typeof C"); 29 30AssertType(this, "typeof C"); 31 return this; 32 } 33} 34 35let t = C.bar(); 36AssertType(t, "typeof C"); 37AssertType(C.bar(), "typeof C"); 38AssertType(C.bar, "() => typeof C"); 39 40// all ok 41let r2 = t.foo + 1; 42AssertType(r2, "number"); 43AssertType(t.foo + 1, "number"); 44AssertType(t.foo, "number"); 45AssertType(1, "int"); 46 47let r3 = t.bar(); 48AssertType(r3, "typeof C"); 49AssertType(t.bar(), "typeof C"); 50AssertType(t.bar, "() => typeof C"); 51 52let r4 = new t(1); 53AssertType(r4, "C"); 54AssertType(new t(1), "C"); 55AssertType(t, "typeof C"); 56AssertType(1, "int"); 57 58class C2<T> { 59 static test: number; 60 constructor(x: string) { } 61 static foo: string; 62 static bar() { 63 // type of this is the constructor function type 64 let t = this; 65AssertType(t, "typeof C2"); 66AssertType(this, "typeof C2"); 67 68AssertType(this, "typeof C2"); 69 return this; 70 } 71} 72 73let t2 = C2.bar(); 74AssertType(t2, "typeof C2"); 75AssertType(C2.bar(), "typeof C2"); 76AssertType(C2.bar, "() => typeof C2"); 77 78// all ok 79let r5 = t2.foo + 1; 80AssertType(r5, "string"); 81AssertType(t2.foo + 1, "string"); 82AssertType(t2.foo, "string"); 83AssertType(1, "int"); 84 85let r6 = t2.bar(); 86AssertType(r6, "typeof C2"); 87AssertType(t2.bar(), "typeof C2"); 88AssertType(t2.bar, "() => typeof C2"); 89 90let r7 = new t2(''); 91AssertType(r7, "C2<unknown>"); 92AssertType(new t2(''), "C2<unknown>"); 93AssertType(t2, "typeof C2"); 94AssertType('', "string"); 95 96 97 98