diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 016e43c9e5d561f6d18a8326d0167ae20709b249..b7df6f2ab23ce3b1f18ef8a80e38408b3a6c9618 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -227,6 +227,11 @@ checker::Type *MemberExpression::TraverseUnionMember(checker::ETSChecker *checke return; } + if(!commonPropType->IsETSFunctionType() || !memberType->IsETSFunctionType()) { + checker->LogError(diagnostic::MEMBER_TYPE_MISMATCH_ACROSS_UNION, {}, Start()); + return; + } + auto newType = checker->IntersectSignatureSets(commonPropType->AsETSFunctionType(), memberType->AsETSFunctionType()); if (newType->AsETSFunctionType()->CallSignatures().empty()) { diff --git a/ets2panda/test/ast/compiler/ets/union_objects_with_different_member_types.ets b/ets2panda/test/ast/compiler/ets/union_objects_with_different_member_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..a421772e1abec60db89f2ebe1195cf325b608224 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_objects_with_different_member_types.ets @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface I { + foo?:() => void; +} + +class A { + foo(): void { + console.log('original'); + } +} + +let u: I | A = new A(); + +function main() { + u.foo = () => { + console.log('test'); + }; + + u.foo(); +} + +/* @@? 29:5 Error TypeError: Member type must be the same for all union objects. */ +/* @@? 33:5 Error TypeError: Member type must be the same for all union objects. */ +/* @@? 33:5 Error TypeError: Value is possibly nullish. */ +/* @@? 33:5 Error TypeError: Type '() => void|undefined' has no call signatures. */