@360works/fmpromise
Version:
A modern JS toolkit for FileMaker Web Viewers, including a dev server and type generation.
42 lines (41 loc) • 1.34 kB
TypeScript
/**
* Defines the structure for a single configuration attribute in a schema.
*/
export interface ConfigAttribute {
label: string;
description?: string;
type: 'field' | 'script' | 'calculation' | 'number' | 'text' | 'color';
required?: boolean;
defaultValue?: any;
}
/**
* A schema is a collection of attributes, keyed by their setting name.
*/
export type ConfigSchema = Record<string, ConfigAttribute>;
/**
* A mapping from the schema's 'type' string to an actual TypeScript type.
* 'field' and 'script' are ultimately strings, but 'calculation' can be anything.
*/
type ConfigTypeMap = {
field: string;
script: string;
calculation: any;
number: number;
text: string;
color: string;
};
/**
* A powerful generic utility type that infers a settings interface from a schema object.
* It correctly handles required and optional properties.
*
* @example
* const mySchema = { value: { type: 'number', required: true }, name: { type: 'text' } };
* type MySettings = SettingsFromSchema<typeof mySchema>;
* // MySettings is now: { value: number; name?: string; }
*/
export type SettingsFromSchema<S extends ConfigSchema> = {
-readonly [K in keyof S]: S[K] extends {
required: true;
} ? ConfigTypeMap[S[K]['type']] : ConfigTypeMap[S[K]['type']] | undefined;
};
export {};