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/collectionPatternNoError.ts === 20declare function AssertType(value:any, type:string):void; 21interface MsgConstructor<T extends Message> { 22 new(data: Array<{}>): T; 23} 24class Message { 25 clone(): this { 26AssertType(this, "this"); 27 return this; 28 } 29} 30interface MessageList<T extends Message> extends Message { 31 methodOnMessageList(): T[]; 32} 33 34function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V { 35AssertType(null!, "null"); 36AssertType(null, "null"); 37 return null!; 38} 39 40class DataProvider<T extends Message, U extends MessageList<T>> { 41 constructor( 42 private readonly message: MsgConstructor<T>, 43 private readonly messageList: MsgConstructor<U>, 44 ) { } 45 46 fetch() { 47 const messageList = fetchMsg(this.messageList); 48AssertType(messageList, "U"); 49AssertType(fetchMsg(this.messageList), "U"); 50AssertType(fetchMsg, "<V extends Message>(MsgConstructor<V>) => V"); 51AssertType(this.messageList, "MsgConstructor<U>"); 52AssertType(this, "this"); 53 54 messageList.methodOnMessageList(); 55AssertType(messageList.methodOnMessageList(), "T[]"); 56AssertType(messageList.methodOnMessageList, "() => T[]"); 57 } 58} 59 60// The same bug as the above but using indexed accesses 61// (won't surface directly unless unsound indexed access assignments are forbidden) 62function f< 63 U extends {TType: MessageList<T>}, 64 T extends Message 65>(message: MsgConstructor<T>, messageList: MsgConstructor<U["TType"]>) { 66 fetchMsg(messageList).methodOnMessageList(); 67AssertType(fetchMsg(messageList).methodOnMessageList(), "T[]"); 68AssertType(fetchMsg(messageList).methodOnMessageList, "() => T[]"); 69AssertType(fetchMsg(messageList), "U["TType"]"); 70AssertType(fetchMsg, "<V extends Message>(MsgConstructor<V>) => V"); 71AssertType(messageList, "MsgConstructor<U["TType"]>"); 72} 73 74 75