diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..cd908a75a38ca 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -5133,7 +5133,11 @@ func (c *Checker) checkInterfaceDeclaration(node *ast.Node) { // run subsequent checks only if first set succeeded if c.checkInheritedPropertiesAreIdentical(t, firstInterfaceDeclaration.Name()) { for _, baseType := range c.getBaseTypes(t) { - c.checkTypeAssignableTo(typeWithThis, c.getTypeWithThisArgument(baseType, t.AsInterfaceType().thisType, false), firstInterfaceDeclaration.Name(), diagnostics.Interface_0_incorrectly_extends_interface_1) + message := diagnostics.Interface_0_incorrectly_extends_interface_1 + if baseType.flags&TypeFlagsTypeParameter != 0 { + message = diagnostics.Interface_0_cannot_extend_type_parameter_1 + } + c.checkTypeAssignableTo(typeWithThis, c.getTypeWithThisArgument(baseType, t.AsInterfaceType().thisType, false), firstInterfaceDeclaration.Name(), message) } c.checkIndexConstraints(t, symbol /*isStaticIndex*/, false) } diff --git a/tsc/internal/diagnostics/diagnosticMessages.generated.json b/tsc/internal/diagnostics/diagnosticMessages.generated.json index 300558dd1c833..e4fde50233eba 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.generated.json +++ b/tsc/internal/diagnostics/diagnosticMessages.generated.json @@ -1009,6 +1009,7 @@ "This_expression_is_never_nullish_2881": "This expression is never nullish.", "Cannot_find_module_or_type_declarations_for_side_effect_import_of_0_2882": "Cannot find module or type declarations for side-effect import of '{0}'.", "The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_2883": "The inferred type of '{0}' cannot be named without a reference to '{2}' from '{1}'. This is likely not portable. A type annotation is necessary.", + "Interface_0_cannot_extend_type_parameter_1_2884": "Interface '{0}' cannot extend type parameter '{1}'.", "Import_declaration_0_is_using_private_name_1_4000": "Import declaration '{0}' is using private name '{1}'.", "Type_parameter_0_of_exported_class_has_or_is_using_private_name_1_4002": "Type parameter '{0}' of exported class has or is using private name '{1}'.", "Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1_4004": "Type parameter '{0}' of exported interface has or is using private name '{1}'.", diff --git a/tsc/internal/diagnostics/diagnosticMessages.json b/tsc/internal/diagnostics/diagnosticMessages.json index ecf6595ffd641..ce11be7a85936 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.json +++ b/tsc/internal/diagnostics/diagnosticMessages.json @@ -4050,6 +4050,10 @@ "category": "Error", "code": 2883 }, + "Interface '{0}' cannot extend type parameter '{1}'.": { + "category": "Error", + "code": 2884 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tsc/internal/diagnostics/diagnostics_generated.go b/tsc/internal/diagnostics/diagnostics_generated.go index 0d016471319dd..274c98c35703a 100644 --- a/tsc/internal/diagnostics/diagnostics_generated.go +++ b/tsc/internal/diagnostics/diagnostics_generated.go @@ -2022,6 +2022,8 @@ var Cannot_find_module_or_type_declarations_for_side_effect_import_of_0 = &Messa var The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary = &Message{code: 2883, category: CategoryError, key: "The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_2883", text: "The inferred type of '{0}' cannot be named without a reference to '{2}' from '{1}'. This is likely not portable. A type annotation is necessary."} +var Interface_0_cannot_extend_type_parameter_1 = &Message{code: 2884, category: CategoryError, key: "Interface_0_cannot_extend_type_parameter_1_2884", text: "Interface '{0}' cannot extend type parameter '{1}'."} + var Import_declaration_0_is_using_private_name_1 = &Message{code: 4000, category: CategoryError, key: "Import_declaration_0_is_using_private_name_1_4000", text: "Import declaration '{0}' is using private name '{1}'."} var Type_parameter_0_of_exported_class_has_or_is_using_private_name_1 = &Message{code: 4002, category: CategoryError, key: "Type_parameter_0_of_exported_class_has_or_is_using_private_name_1_4002", text: "Type parameter '{0}' of exported class has or is using private name '{1}'."} @@ -5505,6 +5507,7 @@ var allMessages = [...]**Message{ &This_expression_is_never_nullish, &Cannot_find_module_or_type_declarations_for_side_effect_import_of_0, &The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary, + &Interface_0_cannot_extend_type_parameter_1, &Import_declaration_0_is_using_private_name_1, &Type_parameter_0_of_exported_class_has_or_is_using_private_name_1, &Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1, diff --git a/tsc/testdata/baselines/reference/compiler/interfaceExtendsConstrainedTypeParameter.errors.txt b/tsc/testdata/baselines/reference/compiler/interfaceExtendsConstrainedTypeParameter.errors.txt new file mode 100644 index 0000000000000..1681ac7d275d1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/interfaceExtendsConstrainedTypeParameter.errors.txt @@ -0,0 +1,24 @@ +interfaceExtendsConstrainedTypeParameter.ts(1,11): error TS2884: Interface 'Constrained' cannot extend type parameter 'T'. + 'Constrained' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'object'. +interfaceExtendsConstrainedTypeParameter.ts(2,36): error TS2312: An interface can only extend an object type or intersection of object types with statically known members. +interfaceExtendsConstrainedTypeParameter.ts(5,11): error TS2430: Interface 'Derived' incorrectly extends interface 'Base'. + Types of property 'value' are incompatible. + Type 'string' is not assignable to type 'number'. + + +==== interfaceExtendsConstrainedTypeParameter.ts (3 errors) ==== + interface Constrained extends T {} + ~~~~~~~~~~~ +!!! error TS2884: Interface 'Constrained' cannot extend type parameter 'T'. +!!! error TS2884: 'Constrained' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'object'. + interface Unconstrained extends T {} + ~ +!!! error TS2312: An interface can only extend an object type or intersection of object types with statically known members. + + interface Base { value: number } + interface Derived extends Base { value: string } + ~~~~~~~ +!!! error TS2430: Interface 'Derived' incorrectly extends interface 'Base'. +!!! error TS2430: Types of property 'value' are incompatible. +!!! error TS2430: Type 'string' is not assignable to type 'number'. + \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/interfaceExtendsConstrainedTypeParameter.ts b/tsc/testdata/tests/cases/compiler/interfaceExtendsConstrainedTypeParameter.ts new file mode 100644 index 0000000000000..f805ffd23c371 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/interfaceExtendsConstrainedTypeParameter.ts @@ -0,0 +1,7 @@ +// @noEmit: true +// @noTypesAndSymbols: true +interface Constrained extends T {} +interface Unconstrained extends T {} + +interface Base { value: number } +interface Derived extends Base { value: string }