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/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembers2.ts === 20declare function AssertType(value:any, type:string):void; 21// members N and M of types S and T have the same name, same accessibility, same optionality, and N is assignable M 22// additional optional properties do not cause errors 23 24class S { foo: string; } 25class T { foo: string; } 26let s: S; 27AssertType(s, "S"); 28 29let t: T; 30AssertType(t, "T"); 31 32interface S2 { foo: string; bar?: string } 33interface T2 { foo: string; baz?: string } 34let s2: S2; 35AssertType(s2, "S2"); 36 37let t2: T2; 38AssertType(t2, "T2"); 39 40let a: { foo: string; bar?: string 41AssertType(a, "{ foo: string; bar?: string; }"); 42 43AssertType(foo, "string"); 44 45AssertType(bar, "string"); 46} 47 48let b: { foo: string; baz?: string 49AssertType(b, "{ foo: string; baz?: string; }"); 50 51AssertType(foo, "string"); 52 53AssertType(baz, "string"); 54} 55 56let a2 = { foo: '' }; 57AssertType(a2, "{ foo: string; }"); 58AssertType({ foo: '' }, "{ foo: string; }"); 59AssertType(foo, "string"); 60AssertType('', "string"); 61 62let b2 = { foo: '' }; 63AssertType(b2, "{ foo: string; }"); 64AssertType({ foo: '' }, "{ foo: string; }"); 65AssertType(foo, "string"); 66AssertType('', "string"); 67 68s = t; 69AssertType(s = t, "T"); 70AssertType(s, "S"); 71AssertType(t, "T"); 72 73t = s; 74AssertType(t = s, "S"); 75AssertType(t, "T"); 76AssertType(s, "S"); 77 78s = s2; 79AssertType(s = s2, "S2"); 80AssertType(s, "S"); 81AssertType(s2, "S2"); 82 83s = a2; 84AssertType(s = a2, "{ foo: string; }"); 85AssertType(s, "S"); 86AssertType(a2, "{ foo: string; }"); 87 88s2 = t2; 89AssertType(s2 = t2, "T2"); 90AssertType(s2, "S2"); 91AssertType(t2, "T2"); 92 93t2 = s2; 94AssertType(t2 = s2, "S2"); 95AssertType(t2, "T2"); 96AssertType(s2, "S2"); 97 98s2 = t; 99AssertType(s2 = t, "T"); 100AssertType(s2, "S2"); 101AssertType(t, "T"); 102 103s2 = b; 104AssertType(s2 = b, "{ foo: string; baz?: string; }"); 105AssertType(s2, "S2"); 106AssertType(b, "{ foo: string; baz?: string; }"); 107 108s2 = a2; 109AssertType(s2 = a2, "{ foo: string; }"); 110AssertType(s2, "S2"); 111AssertType(a2, "{ foo: string; }"); 112 113a = b; 114AssertType(a = b, "{ foo: string; baz?: string; }"); 115AssertType(a, "{ foo: string; bar?: string; }"); 116AssertType(b, "{ foo: string; baz?: string; }"); 117 118b = a; 119AssertType(b = a, "{ foo: string; bar?: string; }"); 120AssertType(b, "{ foo: string; baz?: string; }"); 121AssertType(a, "{ foo: string; bar?: string; }"); 122 123a = s; 124AssertType(a = s, "S"); 125AssertType(a, "{ foo: string; bar?: string; }"); 126AssertType(s, "S"); 127 128a = s2; 129AssertType(a = s2, "S2"); 130AssertType(a, "{ foo: string; bar?: string; }"); 131AssertType(s2, "S2"); 132 133a = a2; 134AssertType(a = a2, "{ foo: string; }"); 135AssertType(a, "{ foo: string; bar?: string; }"); 136AssertType(a2, "{ foo: string; }"); 137 138a2 = b2; 139AssertType(a2 = b2, "{ foo: string; }"); 140AssertType(a2, "{ foo: string; }"); 141AssertType(b2, "{ foo: string; }"); 142 143b2 = a2; 144AssertType(b2 = a2, "{ foo: string; }"); 145AssertType(b2, "{ foo: string; }"); 146AssertType(a2, "{ foo: string; }"); 147 148a2 = b; 149AssertType(a2 = b, "{ foo: string; baz?: string; }"); 150AssertType(a2, "{ foo: string; }"); 151AssertType(b, "{ foo: string; baz?: string; }"); 152 153a2 = t2; 154AssertType(a2 = t2, "T2"); 155AssertType(a2, "{ foo: string; }"); 156AssertType(t2, "T2"); 157 158a2 = t; 159AssertType(a2 = t, "T"); 160AssertType(a2, "{ foo: string; }"); 161AssertType(t, "T"); 162 163 164