josdejong/ducktype

Are recursive types supported?

Open

#40 geöffnet am 17. Jan. 2021

Auf GitHub ansehen
 (5 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (10 Forks)github user discovery
enhancementhelp wanted

Repository-Metriken

Stars
 (92 Stars)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

Is it possible to define a type e.g. "segment" that is either a string, a number, or an array of segments (recursive definition)? If not, please consider it a feature request.

Something like:

const segment = ducktype.indirect();
segment.that = ducktype(String, Number, [segment]);

or even:

const segment = ducktype.recursive(self => ducktype(String, Number, [self]));

Basically I need to validate that all "leaves" of a non-flat array are of a particular union type. I cannot flatten the array before validating because it is nested in a bigger data structure that I want to validate as a whole (as opposed to picking these arrays out of it for flattening and validation), and the nesting structure within the array is meaningful.

Contributor Guide