-
Notifications
You must be signed in to change notification settings - Fork 2
chore(types): start comprehensive typing #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ Deploy Preview for blobs-js ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@@ -19,6 +19,7 @@ interface NamedStoreOptions extends BaseStoreOptions { | |||
} | |||
|
|||
export type StoreOptions = DeployStoreOptions | NamedStoreOptions | |||
type Key<T> = T extends string ? (T extends '' | `/${string}` ? never : T) : never |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we want to go the route of stronger types, I'd love some input on naming conventions - this is more or less a POC/idea PR for consideration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure what to think about this idea, honestly. I like types like this when it comes to auto-completing static inputs, e.g. when frameworks use this to make their API discoverable. In this case, though, the key will often be something that stems from user input so it isn't static - and I worry that this can lead to hard-to-debug typescript errors, with little benefit at runtime.
What i'm not unsure on is the dog, though - that's a very cute doggo. |
In the cases where they're static, like someone just playing with functions to learn our system or insert contrived example they could still get the benefit
I wonder if this is fair though because we can add doc blocks to the |
You commented out two type errors in the test suite. Both of them say Essentially, we're trying to balance how much people are confused by this type error, against how much they gain by discovering a bug at type-check time as opposed to runtime. Since we're only talking about statically analyzable inputs here, those runtime errors wouldn't depend on user input, so they'd occur pretty fast and probably during local dev. On this tradeoff scale, my gut feeling is that the fancy types do more harm than good because the error messages are hard to read. But maybe there's a way of annotating the type with a custom error message? |
Thanks for the input @Skn0tt - until we get better ways to tell users "why never" it seems like this might not be worth it. |
I actually figured out after closing this that function overloads could provide a better experience async set(key: `Key must not be empty and must not start with /`, data: BlobInput, opts?: SetOptions): Promise<void>
async set<K>(key: Key<K>, data: BlobInput): Promise<void>
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions): Promise<void>
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions = {}) { |
Which problem is this pull request solving?
The type signatures could be improved to provide static checks to our typescript users that supplement our runtime checks.
List other issues or pull requests related to this problem
Noticed with #134
Describe the solution you've chosen
#134 disallows empty keys in the
set
functions at runtime, but I chose to infer what's passed in to those and disallow''
and strings that start with/
at the type level.Checklist
Please add a
x
inside each checkbox:A picture of a cute animal (not mandatory but encouraged)