You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While I am submitting this for guidance, this is a blocking issue me. Errors point out this is a limitation issue, but not how to resolve it. See below for details.
Below is a simplified example of a common pattern in TypeScript using abstract classes (could easily interface instead, it should not matter for this) to group elements of a similar type. In this case, we have the Vehicle abstract class, and it states that allvehicles should contain a properties property. As Vehicle is the most generic structure for vehicles, the type is set to unknown.
Then we define two child classes which extendVehicle. Both Car and Plane will have properties, but we expect their type to be different. We do this so we can call properties on any vehicle, and so when users are instantiating either a Car or Plane, they receive the correct code-hints.
// The most generic, abstract class "vehicle"// This class joins our "vehicles" under a shared typeexportabstractclassVehicle{properties: unknown;}exportclassCarextendsVehicle{properties: CarProperties;constructor(properties: CarProperties){super()this.properties=properties}}exportclassPlaneextendsVehicle{properties: PlaneProperties;constructor(properties: PlaneProperties){super()this.properties=properties}}interfaceCarProperties{doors: number;color: string;}interfacePlaneProperties{wings: number;callSign: string;}
The code above is valid and working TypeScript, but this will not compile under JSII due to JSII5003
// This pattern above exists so that we might// do something like the following:constmyVehicles: Vehicle[]=[]constmyCar=newCar({doors: 2,color: "red"})// proper type code-hints are given hereconstmyPlane=newPlane({wings: 2,callSign: "bravo"})// proper type code-hints are given heremyVehicles.push(myCar)myVehicles.push(myPlane)myVehicles.forEach(vehicle=>{console.log(vehicle.properties.toString())})
Error:
Type model errors prevented the JSII assembly from being created
error JSII5004: "Car#properties" changes the property type to "CarProperties" when overriding Vehicle. Change it to "unknown"
The only way around this I see is to drop the Typing on Car and Plane, which would leave the user blind when instantiating those classes. What is the recommended pattern for dealing with this type of scenario with JSII?
Edit:
I have just discovered #2314 which appears to be the same topic
guidanceQuestion that needs advice or information.
1 participant
Converted from issue
This discussion was converted from issue #3068 on March 31, 2022 23:38.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
❓ Guidance
While I am submitting this for guidance, this is a blocking issue me. Errors point out this is a limitation issue, but not how to resolve it. See below for details.
Affected Languages
TypeScript
orJavascript
Python
Java
C#
,F#
, ...)Go
General Information
The Question
Below is a simplified example of a common pattern in TypeScript using abstract classes (could easily interface instead, it should not matter for this) to group elements of a similar type. In this case, we have the
Vehicle
abstract class, and it states that all vehicles should contain aproperties
property. AsVehicle
is the most generic structure for vehicles, the type is set tounknown
.Then we define two child classes which extend
Vehicle
. BothCar
andPlane
will haveproperties
, but we expect their type to be different. We do this so we can callproperties
on any vehicle, and so when users are instantiating either aCar
orPlane
, they receive the correct code-hints.The code above is valid and working TypeScript, but this will not compile under JSII due to
JSII5003
Error:
The only way around this I see is to drop the Typing on
Car
andPlane
, which would leave the user blind when instantiating those classes. What is the recommended pattern for dealing with this type of scenario with JSII?Edit:
I have just discovered #2314 which appears to be the same topic
Beta Was this translation helpful? Give feedback.
All reactions