Skip to content

Well typing string literals #478

@ShacharHarshuv

Description

@ShacharHarshuv

This suggestion has to do with improving the types, with no functional change.

A lot of string literals in Tonal appear to expect very specific kinds of inputs, but the types show a general string.

For example, the type IntervalName can be improved. Based on the docs, it could be something like this:

type Quality = "dddd" | "ddd" | "dd" | "d" | "m" | "M" | "P" | "A" | "AA" | "AAA" | "AAAA";
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' |'8' | '9';
type Natural = `${Exclude<Digit, '0'>}${Digit | ''}`;
type IntervalName = `${'-' | ''}${Natural}${Quality}`

(Note: I allow only 2 digit numbers but I think that's enough?)

The same thing could be done with chord names, note names, pitch classes, mode names, roman numerals, time durations, time signatures, chord types, scale types etc.

Caveat: some of these entity allow to add custom ones, which means the available types is not known in advance. But with these adding the defaults ones in a union + | string will still at least allow for auto-complete in IDEs, so there is still some value. I think at least for the none extensible ones we shouldn't allow free strings.

I can open a PR for this myself, but first I want to see the opinion of the maintainers of this repository before doing so.

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