Helper types and predicates for type IDs #37
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Types of changes
Description
If one wants to write a proper TypeScript predicate function which ensures that the given string is a proper Custom Type name, it's not sufficient to have a TypeScript type definition
AllDocumentTypes['type']
available, because it's, sadly, literally impossible to infer a JavaScript array of strings out of the union type.In addition to that, it is significant to differentiate between Custom Types which have
uid
s (basically, which are "Repeatable"), and which don't, because, for example, you can't meaningfully pass a Singleton type ID to aclient.getByUID()
call.This PR adds a set of helper types and functions to the generated code which allows, for example, the following:
You already have pretty good type inference in cases where the type ID is hardcoded in the source code, but this validator function helps to narrow types in cases where we don't control the ID value.
Overall, I suggest the following three functions and two accompanying types:
isTypeCode
narrowsstring
toAllDocumentTypes['type']
AnyOurDocumentWithoutUID
which is a subset ofAllDocumentTypes
, where all documents don't have anuid
fieldisTypeCodeForTypeWithoutUIDs
which narrowsstring
toAnyOurDocumentWithoutUID['type']
AnyOurDocumentWithUID
which is a subset ofAllDocumentTypes
, where all documents do have anuid
fieldisTypeCodeForTypeWithUIDs
which narrowsstring
toAnyOurDocumentWithUID['type']
In addition to that, I'd really like to have a type which infers the document type given the type ID provided, something like that:
but I am not that well-versed in TypeScript to be completely sure the above type actually does what it should and it's outside of the scope of this PR.
Regarding the second item in the checklist
I admit that I didn't add the documentation because adding these helpers have been a necessity in my own project and there were no real need for me to bother with that. If you can point me at the place where it's explained how to add the TSDoc lines to the generated code, I'll add them.
Regarding the third item in the checklist
These changes fail the last test case:
I also admit that I'm not that well-versed in using the namespaces in TypeScript so it's outside of my abilities to correctly write the code generation for that according to your intents. Please advise.
Checklist: