-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Add better typing to transform
#90
Comments
Thanks. I don't have much experience with TS. Lemme research on this a bit. |
Nice! If it's useful then, here are some aditional info about those types: With those, you can type transform as-is just by changing transform(xml: string, template: object): Promise<any>; to transform<P extends Props>(xml: string, template: Template<P>): Promise<OutputOf<P>>; My wrapper is called 'parse' and have inverted arguments just for personal preference. The 'template' function does have a more important purpuse: you could use the typed transform('', {
cache_key: '/HotelListResponse/cacheKey',
session_id: '/HotelListResponse/customerSessionId',
hotels: [
'//HotelSummary',
{
hotel_id: 'hotelId',
name: 'name',
rooms: [
'RoomRateDetailsList/RoomRateDetails',
{
room_name: 'roomDescription',
room_type_id: 'roomTypeCode'
}
]
}
]
}) And this will be well typed, but if you factor the entities: const Hotel = {
hotel_id: 'hotelId',
name: 'name',
rooms: [
'RoomRateDetailsList/RoomRateDetails',
{
room_name: 'roomDescription',
room_type_id: 'roomTypeCode'
}
]
}
transform('', {
cache_key: '/HotelListResponse/cacheKey',
session_id: '/HotelListResponse/customerSessionId',
hotels: ['//HotelSummary', Hotel]
}) Then TS will complain that 'Hotel' entity in In the end, this aparentely 'dummy' function: const template = <P extends Props>(props: P): Template<P> => props Has the intent of helping the type system infer the plain object structure as a Template correctly without having to do a cumbersome |
It is possible to statically type the parsed type based on the template argument, I was meaning to do a PR but have no idea how to test it, plus, the definition of 'Template' is possibly incomplete, since I couldn't find doc on it.
I'm using these helpers:
With those, the example in README can be refactored to:
And TS can infer the parsed type:
Or it can be extracted:
Thanks for this awesome project btw
The text was updated successfully, but these errors were encountered: