jsii icon indicating copy to clipboard operation
jsii copied to clipboard

error JSII5003 when overriding methods using identical return types

Open echeung-amzn opened this issue 2 years ago • 0 comments

Describe the bug

Originally posted in https://github.com/aws/jsii/issues/3410#issuecomment-1097127406

In module A:

export type MetricWithAlarmSupport = Metric | MathExpression;

export class MetricFactory {
  adaptMetric(metric: MetricWithAlarmSupport): MetricWithAlarmSupport {
    ...
  }
}

In module B:

import { MetricWithAlarmSupport, MetricFactory } from "module-a";

export class MyMetricFactory extends MetricFactory {
  adaptMetric(metric: MetricWithAlarmSupport): MetricWithAlarmSupport {
    ...
  }
}

Results in errors like:

error JSII5003: "module-b.MyMetricFactory#adaptMetric" changes the return type to "monocdk.aws_cloudwatch.Metric | monocdk.aws_cloudwatch.MathExpression" when overriding module-a.MetricFactory. Change it to "monocdk.aws_cloudwatch.Metric | monocdk.aws_cloudwatch.MathExpression"

Replacing the MetricWithAlarmSupport with Metric | MathExpression results in the same thing.

Expected Behavior

The identical types should be accepted without issue.

Current Behavior

Compiler throws error such as:

[ERROR] jsii/compiler - Type model errors prevented the JSII assembly from being created
error JSII5003: "package-b.ExtendedTestClass#methodWithExplicitTypes" changes the return type to "aws-cdk-lib.aws_cloudwatch.Metric | aws-cdk-lib.aws_cloudwatch.MathExpression" when overriding package-a.TestClass. 

Reproduction Steps

See https://github.com/echeung-amzn/jsii-identical-type-override-issue

Possible Solution

No response

Additional Information/Context

No response

SDK version used

JSII 1.57.0

Environment details (OS name and version, etc.)

macOS 12.3.1, but not relevant

echeung-amzn avatar Apr 20 '22 15:04 echeung-amzn