Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Type definitions #90

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.14.1",
"description": "Perform virtual DOM updates based on changes to a data model.",
"main": "lib/index.js",
"types": "types/etch.d.ts",
"scripts": {
"test": "npm run standard && npm run mocha",
"mocha": "electron-mocha test --renderer --recursive --require @babel/register",
Expand Down Expand Up @@ -41,7 +42,8 @@
"estraverse-fb": "^1.3.2",
"mocha": "^8.1.3",
"random-seed": "^0.3.0",
"standard": "^8.5.0"
"standard": "^8.5.0",
"typescript": "^4.0.3"
},
"standard": {
"parser": "babel-eslint",
Expand Down
6 changes: 6 additions & 0 deletions types/dom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {JSX, EtchJSXElement, EtchExtraProps, Props, ChildSpec, TagSpec, ElementClassConstructor} from "./etch-element"

export function dom<T extends keyof HTMLElementTagNameMap>(tag: T, props?: HTMLElementTagNameMap[T] & EtchExtraProps & Props, ...children: ChildSpec[]): EtchJSXElement
export function dom<T extends keyof SVGElementTagNameMap>(tag: T, props?: SVGElementTagNameMap[T] & EtchExtraProps & Props, ...children: ChildSpec[]): EtchJSXElement
export function dom<T extends JSX.ElementClass>( tag: ElementClassConstructor<T>, props: T["props"],...children: ChildSpec[]): EtchJSXElement
export function dom(tag: string, props?: EtchExtraProps & Props, ...children: ChildSpec[]): EtchJSXElement
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dom.tags are missing. I need to write a script to generate those instead of writing them by hand.

74 changes: 74 additions & 0 deletions types/etch-element.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/** Etch class props */
type Props = object

/** Etch element extra props */
interface EtchExtraProps {
ref?: string
className?: string
on?: {
click?: (e: MouseEvent) => any
[name: string]: ((e: any) => any) | undefined
}
dataset?: {
[propName: string]: string | number | null
}
innerHTML?: string
innerText?: string
key?: any
}

/** Etch HTML element */
export interface EtchHTMLElement<Tag extends keyof HTMLElementTagNameMap> {
tag: Tag
props: HTMLElementTagNameMap[Tag] & EtchExtraProps & Props
children: ChildSpec
ambiguous: Array<object>
}

/** Etch SVG element */
export interface EtchSVGElement<Tag extends keyof SVGElementTagNameMap> {
tag: Tag
props: SVGElementTagNameMap[Tag] & EtchExtraProps & Props
children?: ChildSpec
ambiguous: Array<object>
}


/** Etch element's tag */
type TagSpec = string | ElementClassConstructor<JSX.ElementClass> | keyof HTMLElementTagNameMap | keyof SVGElementTagNameMap

/** General Etch element */
export interface EtchElement<Tag extends TagSpec> {
tag: Tag
props: EtchExtraProps & Props
children?: ChildSpec
ambiguous: Array<object>
}

/** Etch JSX Element */
type EtchJSXElement =
| EtchHTMLElement<keyof HTMLElementTagNameMap>
| EtchSVGElement<keyof SVGElementTagNameMap>
| EtchElement<TagSpec>
| {text: string | number}

type SingleOrArray<T> = T | T[]
/** type of etch elements' children */
type ChildSpec = SingleOrArray<string | number | EtchJSXElement | null>


type ElementClassConstructor<T extends JSX.ElementClass> = new (props: T["props"], children: EtchJSXElement[]) => T

export declare namespace JSX {
type Element = EtchJSXElement
type IntrinsicElements = EtchJSXElement
class ElementClass {
public props: Props
constructor(props: Props, children?: EtchJSXElement[])
public render?(): EtchJSXElement
public update(props: Props, children?: EtchJSXElement[]): Promise<void>
}
interface ElementAttributesProperty {
props: Props // specify the property name to use
}
}
12 changes: 12 additions & 0 deletions types/etch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from "./etch-element"
export * from "./dom"
import {EtchJSXElement} from "./etch-element"

export function destroy(component: any, removeNode?: boolean): Promise<void>
export function destroySync(component: any, removeNode: any): void
export function getScheduler(): any
export function initialize(component: any): void
export function render(virtualNode: EtchJSXElement, options?: any): Node
export function setScheduler(customScheduler: any): void
export function update(component: any, replaceNode?: boolean): Promise<void>
export function updateSync(component: any, replaceNode?: boolean): void