When working with TypeScript you have to do a lot of casting when working with APIs returning Map and List.
Would improve DX considerably if generics are added to these so the APIs can specify what types they contain.
The List and Map types are defined without generics today:
// Before
export type List = Collection & { get(index: number): unknown; ... }
export type Map = { get(key: unknown): unknown; ... }
I created a PR to my fork to show what changes the generated types from the JavaDoc should contain to achieve this.
https://github.com/kling90/sitevision-apps/pull/2/changes
Some code examples
// BEFORE — element type was unknown, has to be cast
const pages = nodeIteratorUtil.findNodes(iterator, filter, 10);
const page = pages.get(0) as Node; // manual cast
// AFTER — List<Node>, fully inferred
const pages = nodeIteratorUtil.findNodes(iterator, filter, 10);
const page = pages.get(0); // Node
const name = page.getName(); // no cast needed
const currentLocale = portletContextUtil.getCurrentLocale();
const translations = translationUtil.getTranslations(node, true);
// BEFORE
const translatedPage = translations.get(currentLocale) as Node; // manual cast
// AFTER
const translatedPage = translations.get(currentLocale); // no cast needed
When working with TypeScript you have to do a lot of casting when working with APIs returning Map and List.
Would improve DX considerably if generics are added to these so the APIs can specify what types they contain.
The List and Map types are defined without generics today:
I created a PR to my fork to show what changes the generated types from the JavaDoc should contain to achieve this.
https://github.com/kling90/sitevision-apps/pull/2/changes
Some code examples