@muban/muban
Version:
Writing components for server-rendered HTML
64 lines (63 loc) • 2.9 kB
TypeScript
import type { Predicate, Primitive, Static } from 'isntnt';
import type { IfAny } from '../Component.types';
import type { RefElementType } from '../refs/refDefinitions.types';
export declare type SourceOption = SourceOptionCss | SourceOptionHtmlText | SourceOptionForm | SourceOptionCustom | {
type?: 'data' | 'json' | 'attr' | 'custom';
target?: string;
name?: string;
};
export declare type SourceOptions = SourceOption | Array<SourceOption>;
export declare type SourceOptionHtmlText = {
type: 'text' | 'html';
target?: string;
};
export declare type SourceOptionForm = {
type: 'form';
target?: string;
name?: string;
formData?: boolean;
};
export declare type SourceOptionCss = {
type: 'css';
target?: string;
name?: string;
options?: {
cssPredicate?: Predicate<string>;
};
};
export declare type SourceOptionCustom = {
type: 'custom';
target?: string;
options?: {
customSource?: (element?: HTMLElement | Array<HTMLElement> | undefined) => any;
};
};
export declare type PropTypeDefinition<T = any> = {
type: typeof Number | typeof String | typeof Boolean | typeof Date | typeof Array | typeof Object | typeof Function;
default?: T extends Primitive ? T : IfAny<T, any, () => T>;
validator?: Predicate<T>;
isOptional?: boolean;
missingValue?: boolean;
shapeType?: Function;
sourceOptions?: SourceOptions;
};
export declare type PropTypeInfo<T = any> = Pick<PropTypeDefinition<T>, 'type' | 'default' | 'validator' | 'isOptional'> & {
name: string;
source: {
name: string;
target: RefElementType | undefined;
} & Pick<SourceOption, 'type'> & Pick<SourceOptionCustom, 'options'> & Pick<SourceOptionCss, 'options'> & Pick<SourceOptionForm, 'formData'>;
};
declare type RequiredPropertyKeys<T> = {
[P in keyof T]: undefined extends T[P] ? never : P;
}[keyof T];
declare type Keys<T> = keyof T;
export declare type ConstructorType<T extends PropTypeDefinition['type']> = InstanceType<T> extends InstanceType<typeof String | typeof Boolean | typeof Number> ? ReturnType<T> : InstanceType<T>;
declare type IsAnyPropTypeDefinition<T extends PropTypeDefinition> = 'default' extends Keys<T> ? IfAny<T['default'], never, 0> : 0;
export declare type ExtractType<T extends PropTypeDefinition> = IsAnyPropTypeDefinition<T> extends never ? any : 'shapeType' extends Keys<T> ? T['shapeType'] : 'validator' extends Keys<T> ? Static<Exclude<T['validator'], undefined>> : ConstructorType<T['type']>;
declare type ExtractOptionalType<T extends PropTypeDefinition, V> = 'missingValue' extends RequiredPropertyKeys<T> ? V | undefined : V;
export declare type TypedProp<T extends PropTypeDefinition> = ExtractOptionalType<T, ExtractType<T>>;
export declare type TypedProps<T extends Record<string, PropTypeDefinition>> = {
[P in keyof T]: TypedProp<T[P]>;
};
export {};