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
On .net it is quite a hassle to use the Terraform CDK properly because of the union types used in TypeScript for many properties. In the .net generation a lot of properties result in a simple object? which removes any type safety and is prone for users to set things correctly.
So I was wondering if there were ideas and/or attempts to get a better type safety by using a fake-union type which gives a certain level of compile time safety.
I prepared a small example on how such a runtime safety could look like. It uses the implicit and explicit casting operators to make the usage more convenient:
// Such a type is the heart of the type safety. we would create one of them for each // number of union types we want to support. e..g Union<T1, T2>, Union<T1, T2, T3, T4> publicclassUnion<T1,T2,T3>{publicenumValue{T1,T2,T3}publicValueFilledValue{get;}publicT1V1{get;}publicT2V2{get;}publicT3V3{get;}publicUnion(T1v1){V1=v1;FilledValue= Value.T1;}publicUnion(T2v2){V2=v2;FilledValue= Value.T2;}publicUnion(T3v3){V3=v3;FilledValue= Value.T3;}publicstaticimplicitoperatorUnion<T1,T2,T3>(T1value){returnnewUnion<T1,T2,T3>(value);}publicstaticimplicitoperatorUnion<T1,T2,T3>(T2value){returnnewUnion<T1,T2,T3>(value);}publicstaticimplicitoperatorUnion<T1,T2,T3>(T3value){returnnewUnion<T1,T2,T3>(value);}publicstaticexplicitoperator T1(Union<T1,T2,T3>value){if(value.FilledValue == Value.T1){return value.V1;}thrownew InvalidCastException("Union did not contain "+typeof(T1).Name);}publicstaticexplicitoperator T2(Union<T1,T2,T3>value){if(value.FilledValue == Value.T2){return value.V2;}thrownew InvalidCastException("Union did not contain "+typeof(T2).Name);}publicstaticexplicitoperator T3(Union<T1,T2,T3>value){if(value.FilledValue == Value.T3){return value.V3;}thrownew InvalidCastException("Union did not contain "+typeof(T3).Name);}}// the generated C# classes will use the union types like thispublicclassResource{publicUnion<string?,string[]?,bool> UnionProp {get;set;}}// this is the usagepublicclassUsage{publicUsage(){// setter (successful)varr1=new Resource
{UnionProp=true};varr2=new Resource
{UnionProp="test"};varr3=new Resource
{UnionProp=new[]{"test"}};// setter which fails on compile time
r1.UnionProp =4711;// getter (successful)varboolValue=(bool)r1.UnionProp;varstringValue=(string?)r2.UnionProp;varstringArrayValue=(string[]?)r3.UnionProp;// getter which will failsvarwrong1=(string?)r1.UnionProp;// will throw an InvalidCastException (like before with object) varwrong2=(bool)r1.UnionProp;// will throw an InvalidCastException (like before with object) varwrong3=(int)r1.UnionProp;// compile time error because it cannot be an int}}
Of course the backend/serialization parts would have to be adjusted accordingly but the benefit for developers would be huge.
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
-
On .net it is quite a hassle to use the Terraform CDK properly because of the union types used in TypeScript for many properties. In the .net generation a lot of properties result in a simple
object?
which removes any type safety and is prone for users to set things correctly.So I was wondering if there were ideas and/or attempts to get a better type safety by using a fake-union type which gives a certain level of compile time safety.
I prepared a small example on how such a runtime safety could look like. It uses the implicit and explicit casting operators to make the usage more convenient:
Of course the backend/serialization parts would have to be adjusted accordingly but the benefit for developers would be huge.
Beta Was this translation helpful? Give feedback.
All reactions