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/inferenceOptionalPropertiesToIndexSignatures.ts === 20declare function AssertType(value:any, type:string):void; 21declare function foo<T>(obj: { [x: string]: T }): T; 22 23declare const x1: { a: string, b: number }; 24AssertType(x1, "{ a: string; b: number; }"); 25AssertType(a, "string"); 26AssertType(b, "number"); 27 28declare const x2: { a: string, b: number | undefined }; 29AssertType(x2, "{ a: string; b: union; }"); 30AssertType(a, "string"); 31AssertType(b, "union"); 32 33declare const x3: { a: string, b?: number }; 34AssertType(x3, "{ a: string; b?: union; }"); 35AssertType(a, "string"); 36AssertType(b, "union"); 37 38declare const x4: { a: string, b?: number | undefined }; 39AssertType(x4, "{ a: string; b?: union; }"); 40AssertType(a, "string"); 41AssertType(b, "union"); 42 43let a1 = foo(x1); // string | number 44AssertType(a1, "union"); 45AssertType(foo(x1), "union"); 46AssertType(foo, "<T>({ [string]: T; }) => T"); 47AssertType(x1, "{ a: string; b: number; }"); 48 49let a2 = foo(x2); // string | number | undefined 50AssertType(a2, "union"); 51AssertType(foo(x2), "union"); 52AssertType(foo, "<T>({ [string]: T; }) => T"); 53AssertType(x2, "{ a: string; b: union; }"); 54 55let a3 = foo(x3); // string | number 56AssertType(a3, "union"); 57AssertType(foo(x3), "union"); 58AssertType(foo, "<T>({ [string]: T; }) => T"); 59AssertType(x3, "{ a: string; b?: union; }"); 60 61let a4 = foo(x4); // string | number 62AssertType(a4, "union"); 63AssertType(foo(x4), "union"); 64AssertType(foo, "<T>({ [string]: T; }) => T"); 65AssertType(x4, "{ a: string; b?: union; }"); 66 67// Repro from #43045 68 69const param2 = Math.random() < 0.5 ? 'value2' : null; 70AssertType(param2, "union"); 71AssertType(Math.random() < 0.5 ? 'value2' : null, "union"); 72AssertType(Math.random() < 0.5, "boolean"); 73AssertType(Math.random(), "number"); 74AssertType(Math.random, "() => number"); 75AssertType(0.5, "double"); 76AssertType('value2', "string"); 77AssertType(null, "null"); 78 79const obj = { 80AssertType(obj, "{ param2?: union; param1: string; }"); 81AssertType({ param1: 'value1', ...(param2 ? {param2} : {})}, "{ param2?: union; param1: string; }"); 82 83 param1: 'value1', 84AssertType(param1, "string"); 85AssertType('value1', "string"); 86 87 ...(param2 ? {param2} : {}) 88AssertType((param2 ? {param2} : {}), "union"); 89AssertType(param2 ? {param2} : {}, "union"); 90AssertType(param2, "union"); 91AssertType({param2}, "{ param2: string; }"); 92AssertType(param2, "string"); 93AssertType({}, "{}"); 94 95}; 96 97const query = Object.entries(obj).map( 98AssertType(query, "string"); 99AssertType(Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&'), "string"); 100AssertType(Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`).join, "(?union) => string"); 101 102 ([k, v]) => `${k}=${encodeURIComponent(v)}` 103).join('&'); 104AssertType('&', "string"); 105 106 107