Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings for argument childProps in Properties.children() #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion lib/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Properties<GP, CP, TP, SP, OP> {
return value;
}

async children<T extends keyof CP>(child: T, childProps?: string[]): Promise<T extends keyof TP ? TP[T] : CP[T]> {
async children<TName extends keyof CP>(child: TName, childProps?: InitialProps<TName,CP, TP>[]): Promise<PropertyType<TName,TP,CP>> {
let initialProps;

if (this.childrenInitialProps) {
Expand Down Expand Up @@ -118,3 +118,42 @@ export class Properties<GP, CP, TP, SP, OP> {
return this.ableton.callMultiple(this.path, calls, this._id, timeout);
}
}

type InitialProps<TName extends keyof CP, CP, TP> =
| ChildProps<TName, CP, TP>
| ChildChildren<TName, CP, TP>;

type ChildProps<TName extends keyof CP, CP, TP> = FlatPropertyType<
TName,
TP,
CP
> extends Properties<infer GP, any, any, any, any>
? keyof GP
: never;

type ChildChildren<TName extends keyof CP, CP, TP> = FlatPropertyType<
TName,
TP,
CP
> extends Properties<any, infer _CP, infer _TP, any, any>
? ChildChildrenDescriptor<_CP, _TP, keyof _CP>
: FlatPropertyType<
TName,
TP,
CP
>;

type ChildChildrenDescriptor<_CP, _TP, TName extends keyof _CP = any> = {
name: TName;
initialProps: InitialProps<TName, _CP, _TP>[];
};

type PropertyType<TName extends keyof CP, TP, CP> = TName extends keyof TP
? TP[TName]
: CP[TName];

type FlatPropertyType<TName extends keyof CP, TP, CP> = TName extends keyof TP
? Flatten<TP[TName]>
: Flatten<CP[TName]>;

type Flatten<T> = T extends Array<infer U> ? Flatten<U> : T;