scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
56 lines (49 loc) • 1.76 kB
text/typescript
import {WidgetFamily} from '../../../types';
// Infer element types from ListWidget method return values
type AddMethodKeys = Extract<keyof ListWidget, `add${string}`>;
type ReturnTypes = {
[K in AddMethodKeys]: ListWidget[K] extends (...args: any[]) => infer R ? R : never;
};
// Widget types and interfaces
export type WidgetElements = ReturnTypes[AddMethodKeys][];
export type WidgetPadding = (typeof ListWidget.prototype)['setPadding'] extends (
top: infer T,
leading: infer L,
bottom: infer B,
trailing: infer R,
) => void
? {top: T; leading: L; bottom: B; trailing: R}
: never;
// Infer background related types from ListWidget interface
type BackgroundProps = Pick<ListWidget, 'backgroundColor' | 'backgroundImage' | 'backgroundGradient'>;
export type WidgetBackground = {
[K in keyof BackgroundProps]: {
type: K extends 'backgroundColor' ? 'color' : K extends 'backgroundImage' ? 'image' : 'gradient';
value: BackgroundProps[K];
};
}[keyof BackgroundProps];
// Infer alignment methods from WidgetStack
type AlignmentMethods = Extract<keyof WidgetStack, `${string}AlignContent`>;
export type WidgetAlignment = {
[K in AlignmentMethods as K extends `${infer Base}AlignContent` ? Base : never]: boolean;
};
// Infer content mode from WidgetImage
type ContentModeProps = Pick<WidgetImage, 'resizable' | 'imageSize'>;
export type WidgetContentMode = {
[K in keyof ContentModeProps]: boolean;
};
// Infer complete configuration type from ListWidget
export type ListWidgetConfig = {
family: WidgetFamily;
} & Pick<
ListWidget,
| 'backgroundColor'
| 'backgroundImage'
| 'backgroundGradient'
| 'spacing'
| 'url'
| 'refreshAfterDate'
| 'addAccessoryWidgetBackground'
> & {
padding: WidgetPadding;
};