Skip to content

Call for feedback on API design #20

@ColinEberhardt

Description

@ColinEberhardt

Based on the current implementation progress, the PlainDate and PlainDateTime types are now relatively complete. These should be useful for anyone wanting to perform non-timezone aware date and time manipulation.

Before making a v1.0 release I am keen to stabilise the API. The biggest challenge here is dealing with union types. For example, the first arg of the PlainDate.add method unions three different types:

add(durationLike: Temporal.Duration | DurationLike | string, options?: ArithmeticOptions);

This allowing you to do the following:

const date = new PlainDate(...);
// add one day, three different ways
date.add(new Duration(0, 0, 0, 1));
date.add("1D");
date.add({ days: 1 });

Unfortunately AssemblyScript doesn't support union types so we need to use a different technique here.

There are a few options this library could employ:

1. Static Type Checks

Use a generic type then perform static type checks to determine the type.

Unfortunately the compiler requires type information, which must be provided as follows:

const date = new PlainDate(...);
// add one day, three different ways
date.add<Duration>(new Duration(0, 0, 0, 1));
date.add<string>("1D");
date.add<DurationLike>({ days: 1 });

2. Create multiple API methods

This method could be split into three, with the one that is considered the most frequently used given the plain add form:

const date = new PlainDate(...);
// add one day, three different ways
date.add({ days: 1 });
date.addDuration(new Duration(0, 0, 0, 1));
date.addString("1D");

What do people prefer? Any other ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions