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/controlFlow/controlFlowInstanceofExtendsFunction.ts === 20declare function AssertType(value:any, type:string):void; 21declare global { 22 interface Function { 23 now(): string; 24 } 25} 26 27Function.prototype.now = function () { 28AssertType(Function.prototype.now, "() => string"); 29AssertType(function () { return "now"}, "() => string"); 30AssertType(Function.prototype.now = function () { return "now"}, "() => string"); 31 32AssertType("now", "string"); 33 return "now" 34} 35 36class X { 37 static now() { 38AssertType({}, "{}"); 39 return { 40} 41 } 42 43 why() { 44 45 } 46} 47 48class Y { 49 50} 51 52console.log(X.now()) // works as expected 53AssertType(console.log(X.now()), "void"); 54AssertType(console.log, "(...any[]) => void"); 55AssertType(X.now(), "{}"); 56AssertType(X.now, "() => {}"); 57 58console.log(Y.now()) // works as expected 59AssertType(console.log(Y.now()), "void"); 60AssertType(console.log, "(...any[]) => void"); 61AssertType(Y.now(), "string"); 62AssertType(Y.now, "() => string"); 63 64export const x: X | number = Math.random() > 0.5 ? new X() : 1 65AssertType(x, "union"); 66AssertType(Math.random() > 0.5 ? new X() : 1, "union"); 67AssertType(Math.random() > 0.5, "boolean"); 68AssertType(Math.random(), "number"); 69AssertType(Math.random, "() => number"); 70AssertType(0.5, "double"); 71AssertType(new X(), "X"); 72AssertType(X, "typeof X"); 73AssertType(1, "int"); 74 75if (x instanceof X) { 76 x.why() // should compile 77AssertType(x.why(), "void"); 78AssertType(x.why, "() => void"); 79} 80 81