sam-goodwin / punchcard

Type-safe AWS infrastructure.
Apache License 2.0
507 stars 20 forks source link

feat(shape): add support for any type #37

Closed sam-goodwin closed 5 years ago

sam-goodwin commented 5 years ago

Closes #13

This PR adds support for an any type:

Added an example of how to use it with DynamoDB:

To make use of the update and condition expressions, you first cast the any attribute to a known type and then use that type to build the expressions. E.g. item.anyAttribute.as(string()).equals('a string'). This is a simple approach that maintains type-safety and makes use of the existing utilities.

https://github.com/sam-goodwin/punchcard/blob/4a9215fcf8c4d4bbaa94a5c33f6b7af76a22a8f5/examples/lib/dynamodb.ts#L57-L110

pocesar commented 5 years ago

looks really nice! the type-safe if is just beautiful. just a nit-pick: any should actually, before as call should be of unknown to be extra safe about the versatile shape of it or primitive type, so you need to actually typecast to something, but not terrible necessary, honestly. can be later inferred from the parameters that TS automatically changes from unknown to concrete type

sam-goodwin commented 5 years ago

looks really nice! the type-safe if is just beautiful. just a nit-pick: any should actually, before as call should be of unknown to be extra safe about the versatile shape of it or primitive type, so you need to actually typecast to something, but not terrible necessary, honestly. can be later inferred from the parameters that TS automatically changes from unknown to concrete type

Oh, I missed this suggestion. Do you mean it should be Type<unknown> instead of Type<any>?

pocesar commented 5 years ago

yeah, any is permissive, while unknown is not, by default, but accepts anything when you typecast. the generic would actually fallback to unknown, like in <T extends any = unknown>(arg: T) instead of (arg: unknown)

sam-goodwin commented 5 years ago

I was not aware of unknown's use-cases! This is awesome. I love that we get the dynamic benefits of any with extra safety. Thanks for pointing this out.

sam-goodwin commented 5 years ago

yeah, any is permissive, while unknown is not, by default, but accepts anything when you typecast. the generic would actually fallback to unknown, like in <T extends any = unknown>(arg: T) instead of (arg: unknown)

Can you explain this syntax: T extends any = unknown. Specifically, the any = unknown. I've seen that but haven't found any docs explaining it.

pocesar commented 5 years ago

when setting an any to an unknown means that, although it can accept anything as a parameter, if you don't, it'll fall back for the safe any version, that is unknown. never is used as a non-existent type and can never be assigned to anything. the assignable type counterpart is unknown, and you can typecast mindfully something as unknown as SomeOtherType when you're sure what you're doing. so it's permissible if you provide a typecast, otherwise it acts like a never so, T can be anything, but you must provide a type by yourself, otherwise T = unknown, that the compiler won't allow you to compile (I know, the new types are confusing 😆 but use cases always show up. next in queue is the fallback to infer that should arrive soon)