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
exporttypeExample=IExample;// actual generated typeexporttypeIExample={[kinstring] ?: number};
It's a bit difficult to use these types, since indexing IExample will give a number | undefined value, even though the ToJSON instance for Data.Map should never include undefined values.
constfoo: Example={bar : undefined}// no errorfunctionexample(data: Example){constbar: {[k : string] : number}=data;// Type 'IExample' is not assignable to type '{ [k: string]: number; }'.// 'string' index signatures are incompatible.// Type 'number | undefined' is not assignable to type 'number'.// Type 'undefined' is not assignable to type 'number'.}
Instead, I would have expected the generated type to look more like this:
// desired typeexporttypeIExample={[k : string] : number};constfoo: Example={bar : undefined}// Type 'undefined' is not assignable to type 'number'.functionexample(data: Example){constbar: {[k : string] : number}=data;// Type 'IExample' is not assignable to type '{ [k: string]: number; }'.// 'string' index signatures are incompatible.// Type 'number | undefined' is not assignable to type 'number'.// Type 'undefined' is not assignable to type 'number'.}
Questions
was there a particular reason for this choice of types?
would it be possible to change the behavior when the key is String or an alias for String?
Hmm, the question mark was introduced in this commit: 64c03f3
It's been a while so I don't remember the context well. It makes sense that : was changed to in but I'm not sure why the question mark was needed. I can't think of any situation where such a map would contain undefined.
I tried removing it locally and the tests passed ¯\(ツ)/¯. Trying it in CI here.
Before merging such a change I'd just want to think really hard and see if there's any way aeson can encode a Map or HashMap to have undefined values, in a way that isn't due to the value itself encoding to undefined...
This allows to have unions as keys, which often happens when encoding variants made out of nullary constructors.
It sounds like string keys shouldn't ever cause this. I agree that it'd be great to not have the ? for string keys. Or maybe more generally, for types where there's no possibility of undefined.
Thanks for
aeson-typescript
, it's great!Example
I've noticed that when using
Data.Map
as follows:(helper functions)
The generated types look like this:
It's a bit difficult to use these types, since indexing
IExample
will give anumber | undefined
value, even though the ToJSON instance forData.Map
should never includeundefined
values.Note that these errors still happen with
exactOptionalPropertyTypes
enabled.Desired Behavior
Instead, I would have expected the generated type to look more like this:
Questions
String
or an alias forString
?Related
microsoft/TypeScript#46969
The text was updated successfully, but these errors were encountered: