question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript union types are not correctly resolved in Java

See original GitHub issue

In Typescript CloudFormationStackArtifact#getAssets returns AssetMetadataEntry[], where AssetMetadataEntry = FileAssetMetadataEntry | ContainerImageAssetMetadataEntry. In Java this method returns a List<Object>, which is, actually, a list of JSII objects. The JSII objects are supposed to reference FileAssetMetadataEntry and ContainerImageAssetMetadataEntry objects from JSII runtime, however the reference type is always FileAssetMetadataEntry:

final Object asset = stackArtifact.getAssets().get(0);
// the reference interface will always be FileAssetMetadataEntry even though it's actually ContainerImageAssetMetadataEntry
JsiiEngine.getInstance().nativeToObjRef(asset).getInterfaces() 

The issue is also actual for software.amazon.awscdk.cloudassembly.schema.MissingContext#getProps. It is supposed to return ContextQueryProperties which is a union type in Typescript, however, the JSII object reference is always of type AmiContextQuery (looks like it always takes the first type in the union type definition as both FileAssetMetadataEntry and AmiContextQuery are defined first.

Reproduction Steps

  1. Define a container image asset in your stack
  2. Synthesize the templates
  3. Try to access repositoryName of the asset:
Object asset = stackArtifact.getAssets().get(0);
JsiiEngine jsiiEngine = JsiiEngine.getInstance();
JsiiObjectRef objectRef = jsiiEngine.nativeToObjRef(asset);
JsiiClient client = jsiiEngine.getClient();
String packaging = client.getPropertyValue(objectRef, "packaging").asText();
assert packaging.equals("container-image");
String repositoryName = client.getPropertyValue(objectRef, "repositoryName").asText(); //causes an exception

Error Log

Error: Type Object or interface(s) @aws-cdk/cloud-assembly-schema.FileAssetMetadataEntry doesn't have a property 'repositoryName'
    at Kernel._typeInfoForProperty (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:8205:19)
    at Kernel.get (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7642:25)
    at KernelHost.processRequest (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7388:28)
    at KernelHost.run (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7328:14)
    at Immediate._onImmediate (/private/var/folders/g6/wh2_ynfn4nb9wcpv5vjfphww0000gn/T/jsii-java-runtime14150496997154827187/jsii-runtime.js:7331:37)
    at processImmediate (internal/timers.js:456:21)

Environment

  • CLI Version : 1.41.0
  • Framework Version: 1.41.0
  • OS : macOs Catalina
  • Language : Java

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RoKishcommented, May 28, 2020

Thank you! Just in case, I’ve created a repo reproducing the issue: https://github.com/RoKish/cdk-union-type-bug.

0reactions
RomainMullercommented, Aug 29, 2022

I’ll mention here that we have an open RFC about the type unions problem => https://github.com/aws/aws-cdk-rfcs/issues/193

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the most out of TypeScript - Union Types
First thing's first, TypeScript is not OOP. ... However, lets move on to union types, because in my opinion, this is a better...
Read more >
Union types should not have too many elements
When a union type is used for a function parameter and it is accepting too many types, it may indicate the function is...
Read more >
Why is type inference not working on union types when one of ...
When I use a union type for which one of the type is any , the TypeScript editor does not seem to resolve...
Read more >
Handbook - Unions and Intersection Types - TypeScript
How to use unions and intersection types in TypeScript. ... Argument of type 'boolean' is not assignable to parameter of type 'string |...
Read more >
Documentation - Everyday Types - TypeScript
The primitives: string , number , and boolean · Arrays · any · Type Annotations on Variables · Functions · Object Types ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found