Skip to content

Add generics to List and Map types #201

Description

@kling90

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions