van-hmr/van.d.ts

74 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-02-06 13:17:36 -05:00
declare global
{
namespace VanX
{
2025-02-06 14:32:07 -05:00
type StateOf<T> = { readonly [K in keyof T]: Van.State<T[K]> }
type ValueType<T> = T extends (infer V)[] ? V : T[keyof T]
type KeyType<T> = T extends unknown[] ? number : string
type ReplacementFunc<T> =
T extends (infer V)[] ? (items: V[]) => readonly V[] :
(items: [string, T[keyof T]][]) => readonly [string, T[keyof T]][]
2025-02-06 13:17:36 -05:00
}
2025-02-06 12:38:10 -05:00
2025-02-06 13:17:36 -05:00
namespace Van {
2025-02-06 14:32:07 -05:00
interface State<T> {
val: T
readonly oldVal: T
readonly rawVal: T
}
// Defining readonly view of State<T> for covariance.
// Basically we want StateView<string> to implement StateView<string | number>
type StateView<T> = Readonly<State<T>>
type Val<T> = State<T> | T
type Primitive = string | number | boolean | bigint
// deno-lint-ignore no-explicit-any
type PropValue = Primitive | ((e: any) => void) | null
type PropValueOrDerived = PropValue | StateView<PropValue> | (() => PropValue)
type Props = Record<string, PropValueOrDerived> & { class?: PropValueOrDerived; is?: string }
type PropsWithKnownKeys<ElementType> = Partial<{[K in keyof ElementType]: PropValueOrDerived}>
type ValidChildDomValue = Primitive | Node | null | undefined
type BindingFunc = ((dom?: Node) => ValidChildDomValue) | ((dom?: Element) => Element)
type ChildDom = ValidChildDomValue | StateView<Primitive | null | undefined> | BindingFunc | readonly ChildDom[]
type TagFunc<Result> = (first?: Props & PropsWithKnownKeys<Result> | ChildDom, ...rest: readonly ChildDom[]) => Result
type Tags = Readonly<Record<string, TagFunc<Element>>> & {
[K in keyof HTMLElementTagNameMap]: TagFunc<HTMLElementTagNameMap[K]>
}
2025-02-06 13:17:36 -05:00
}
2025-02-06 14:32:07 -05:00
const van:MembersVan;
const vanX:MembersVanX;
}
2025-02-06 13:17:36 -05:00
2025-02-06 14:32:07 -05:00
type MembersVan = {
readonly state: <T>(initVal: T)=> Van.State<T>
readonly derive: <T>(f: () => T) => Van.State<T>
readonly add: (dom: Element, ...children: readonly Van.ChildDom[]) => Element
readonly tags: Van.Tags & ((namespaceURI: string) => Readonly<Record<string, Van.TagFunc<Element>>>)
readonly hydrate: <T extends Node>(dom: T, f: (dom: T) => T | null | undefined) => T
}
type MembersVanX = {
readonly calc: <R>(f: () => R) => R
readonly reactive: <T extends object>(obj: T) => T
readonly noreactive: <T extends object>(obj: T) => T
readonly stateFields: <T extends object>(obj: T) => VanX.StateOf<T>
readonly raw: <T extends object>(obj: T) => T
readonly list: <T extends object, ElementType extends Element>(container: (() => ElementType) | ElementType, items: T,itemFunc: (v: Van.State<VanX.ValueType<T>>, deleter: () => void, k: VanX.KeyType<T>) => Node) => ElementType
readonly replace: <T extends object>(obj: T, replacement: VanX.ReplacementFunc<T> | T) => T
readonly compact: <T extends object>(obj: T) => T
}
export const Van: MembersVan;
export const VanX: MembersVanX;