Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
icylace committed May 4, 2021
1 parent 04624ad commit 3e73842
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 192 deletions.
26 changes: 20 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare module "hyperapp" {
tag: NonEmptyString<T>,
props: CustomPayloads<S, C> & Props<S>,
children?: MaybeVNode<S> | readonly MaybeVNode<S>[]
): VNode<S>
): ElementVNode<S>

// `memo()` stores a view along with any given data for it.
function memo<S, D extends Indexable = Indexable>(
Expand All @@ -47,10 +47,10 @@ declare module "hyperapp" {
): VNode<S>

// `text()` creates a virtual DOM node representing plain text.
function text<S, T = unknown>(
function text<T = unknown>(
// Values, aside from symbols and functions, can be handled.
value: T extends symbol | ((..._: unknown[]) => unknown) ? never : T
): VNode<S>
): TextVNode

// ---------------------------------------------------------------------------

Expand All @@ -63,7 +63,7 @@ declare module "hyperapp" {
tag: NonEmptyString<T>,
props: CustomPayloads<S, C> & Props<S>,
children?: MaybeVNode<S> | readonly MaybeVNode<S>[]
): VNode<S>
): ElementVNode<S>
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -180,7 +180,7 @@ declare module "hyperapp" {
type Unsubscribe = () => void

// A virtual DOM node (a.k.a. VNode) represents an actual DOM element.
type VNode<S> = {
type ElementVNode<S> = {
readonly props: Props<S>
readonly children: readonly MaybeVNode<S>[]
node: null | undefined | Node
Expand All @@ -203,12 +203,26 @@ declare module "hyperapp" {

// VNode types are based on actual DOM node types:
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
readonly type: 1 | 3
readonly type: 1

// `_VNode` is a phantom guard property which gives us a way to tell `VNode`
// objects apart from `Props` objects. Since we don't expect users to make
// their own VNodes manually, we can take advantage of this trick which
// is unique to TypeScript type definitions for JavaScript code.
_VNode: true
}

// Certain VNodes specifically represent Text nodes and don't rely on state.
type TextVNode = {
readonly props: {}
readonly children: []
node: null | undefined | Node
readonly key: undefined
readonly tag: string
readonly type: 3
_VNode: true
}

// VNodes may represent either Text or Element nodes.
type VNode<S> = ElementVNode<S> | TextVNode
}
File renamed without changes.
234 changes: 117 additions & 117 deletions test/api/h/h.test.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/api/memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ memo<number>((data) => h("div", {}, text(data)), "hi")
// $ExpectType VNode<string>
memo<string>((data) => h("div", {}, text(data)), "hi")

h("div", {}, memo(text, ["hi"])) // $ExpectType VNode<unknown>
h("div", {}, memo(text, ["hi"])) // $ExpectType ElementVNode<unknown>
78 changes: 39 additions & 39 deletions test/api/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { text } from "hyperapp"

text() // $ExpectError
text(true) // $ExpectType VNode<unknown>
text(false) // $ExpectType VNode<unknown>
text(0) // $ExpectType VNode<unknown>
text(2424) // $ExpectType VNode<unknown>
text(-123) // $ExpectType VNode<unknown>
text(-Infinity) // $ExpectType VNode<unknown>
text(Infinity) // $ExpectType VNode<unknown>
text(NaN) // $ExpectType VNode<unknown>
text("") // $ExpectType VNode<unknown>
text("hi") // $ExpectType VNode<unknown>
text(new String("")) // $ExpectType VNode<unknown>
text(new String("hi")) // $ExpectType VNode<unknown>
text({}) // $ExpectType VNode<unknown>
text(new Set()) // $ExpectType VNode<unknown>
text([]) // $ExpectType VNode<unknown>
text(true) // $ExpectType TextVNode
text(false) // $ExpectType TextVNode
text(0) // $ExpectType TextVNode
text(2424) // $ExpectType TextVNode
text(-123) // $ExpectType TextVNode
text(-Infinity) // $ExpectType TextVNode
text(Infinity) // $ExpectType TextVNode
text(NaN) // $ExpectType TextVNode
text("") // $ExpectType TextVNode
text("hi") // $ExpectType TextVNode
text(new String("")) // $ExpectType TextVNode
text(new String("hi")) // $ExpectType TextVNode
text({}) // $ExpectType TextVNode
text(new Set()) // $ExpectType TextVNode
text([]) // $ExpectType TextVNode
text(Symbol()) // $ExpectError
text(() => { }) // $ExpectError
text(null) // $ExpectType VNode<unknown>
text(undefined) // $ExpectType VNode<unknown>
text(null) // $ExpectType TextVNode
text(undefined) // $ExpectType TextVNode

text<number>() // $ExpectError
text<number>(true) // $ExpectType VNode<number>
text<number>(false) // $ExpectType VNode<number>
text<number>(0) // $ExpectType VNode<number>
text<number>(2424) // $ExpectType VNode<number>
text<number>(-123) // $ExpectType VNode<number>
text<number>(-Infinity) // $ExpectType VNode<number>
text<number>(Infinity) // $ExpectType VNode<number>
text<number>(NaN) // $ExpectType VNode<number>
text<number>("") // $ExpectType VNode<number>
text<number>("hi") // $ExpectType VNode<number>
text<number>(new String("")) // $ExpectType VNode<number>
text<number>(new String("hi")) // $ExpectType VNode<number>
text<number>({}) // $ExpectType VNode<number>
text<number>(new Set()) // $ExpectType VNode<number>
text<number>([]) // $ExpectType VNode<number>
// text<number>() // $ExpectError
// text<number>(true) // $ExpectType TextVNode
// text<number>(false) // $ExpectType TextVNode
// text<number>(0) // $ExpectType TextVNode
// text<number>(2424) // $ExpectType TextVNode
// text<number>(-123) // $ExpectType TextVNode
// text<number>(-Infinity) // $ExpectType TextVNode
// text<number>(Infinity) // $ExpectType TextVNode
// text<number>(NaN) // $ExpectType TextVNode
// text<number>("") // $ExpectType TextVNode
// text<number>("hi") // $ExpectType TextVNode
// text<number>(new String("")) // $ExpectType TextVNode
// text<number>(new String("hi")) // $ExpectType TextVNode
// text<number>({}) // $ExpectType TextVNode
// text<number>(new Set()) // $ExpectType TextVNode
// text<number>([]) // $ExpectType TextVNode

// TODO:
text<number>(Symbol()) // $ExpectError
// // TODO:
// text<number>(Symbol()) // $ExpectError

// TODO:
text<number>(() => { }) // $ExpectError
// // TODO:
// text<number>(() => { }) // $ExpectError

text<number>(null) // $ExpectType VNode<number>
text<number>(undefined) // $ExpectType VNode<number>
// text<number>(null) // $ExpectType TextVNode
// text<number>(undefined) // $ExpectType TextVNode
8 changes: 4 additions & 4 deletions test/architecture/actions/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ app<Foo>({ init: [AddSome, 42] })
// $ExpectError
h("button", { onclick: AddSome }, text("add event?!"))

// $ExpectType VNode<Foo>
// $ExpectType ElementVNode<Foo>
h("button", { onclick: [AddSome, 32] }, text("add 32"))

// $ExpectError
Expand All @@ -139,7 +139,7 @@ const AddSomeMore: Action<AppState, number> =
// $ExpectError
h("button", { onclick: AddSomeMore }, text("add event?!"))

// $ExpectType VNode<AppState>
// $ExpectType ElementVNode<AppState>
h("button", { onclick: [AddSomeMore, 32] }, text("add 32"))

// $ExpectError
Expand Down Expand Up @@ -180,7 +180,7 @@ type State = {
const AddFoo: Action<State, number> = (state, amount) => ({...state, foo: state.foo + amount})
const SetName: Action<State, string> = (state, name) => ({...state, name})

// $ExpectType VNode<State>
// $ExpectType ElementVNode<State>
h("div", {
onmousedown: [AddFoo, 32],
onmouseup: [SetName, "unknown"],
Expand All @@ -200,7 +200,7 @@ h("div", {
onmouseleave: [SetName, 32],
})

// $ExpectType VNode<State>
// $ExpectType ElementVNode<State>
h("div", { onmouseenter: (state: State, payload: MouseEvent) => state })

// $ExpectError
Expand Down
50 changes: 25 additions & 25 deletions test/typedh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,28 @@ const h22: TypedH<S22> = ha
const h23: TypedH<S23> = ha // $ExpectError
const h24: TypedH<S24> = ha

h0("br", {}) // $ExpectType VNode<false>
h1("br", {}) // $ExpectType VNode<true>
h2("br", {}) // $ExpectType VNode<boolean>
h3("br", {}) // $ExpectType VNode<0>
h4("br", {}) // $ExpectType VNode<2424>
h5("br", {}) // $ExpectType VNode<-123>
h6("br", {}) // $ExpectType VNode<number>
h7("br", {}) // $ExpectType VNode<"">
h8("br", {}) // $ExpectType VNode<"hi">
h9("br", {}) // $ExpectType VNode<string>
h10("br", {}) // $ExpectType VNode<S10>
h11("br", {}) // $ExpectType VNode<S11>
h12("br", {}) // $ExpectType VNode<S12>
h13("br", {}) // $ExpectType VNode<S13>
h14("br", {}) // $ExpectType VNode<S14>
h15("br", {}) // $ExpectType VNode<S15>
h16("br", {}) // $ExpectType VNode<S16>
h17("br", {}) // $ExpectType VNode<S17>
h18("br", {}) // $ExpectType VNode<[]>
h19("br", {}) // $ExpectType VNode<symbol>
h20("br", {}) // $ExpectType VNode<S20>
h21("br", {}) // $ExpectType VNode<null>
h22("br", {}) // $ExpectType VNode<undefined>
h23("br", {}) // $ExpectType VNode<unknown>
h24("br", {}) // $ExpectType VNode<any>
h0("br", {}) // $ExpectType ElementVNode<false>
h1("br", {}) // $ExpectType ElementVNode<true>
h2("br", {}) // $ExpectType ElementVNode<boolean>
h3("br", {}) // $ExpectType ElementVNode<0>
h4("br", {}) // $ExpectType ElementVNode<2424>
h5("br", {}) // $ExpectType ElementVNode<-123>
h6("br", {}) // $ExpectType ElementVNode<number>
h7("br", {}) // $ExpectType ElementVNode<"">
h8("br", {}) // $ExpectType ElementVNode<"hi">
h9("br", {}) // $ExpectType ElementVNode<string>
h10("br", {}) // $ExpectType ElementVNode<S10>
h11("br", {}) // $ExpectType ElementVNode<S11>
h12("br", {}) // $ExpectType ElementVNode<S12>
h13("br", {}) // $ExpectType ElementVNode<S13>
h14("br", {}) // $ExpectType ElementVNode<S14>
h15("br", {}) // $ExpectType ElementVNode<S15>
h16("br", {}) // $ExpectType ElementVNode<S16>
h17("br", {}) // $ExpectType ElementVNode<S17>
h18("br", {}) // $ExpectType ElementVNode<[]>
h19("br", {}) // $ExpectType ElementVNode<symbol>
h20("br", {}) // $ExpectType ElementVNode<S20>
h21("br", {}) // $ExpectType ElementVNode<null>
h22("br", {}) // $ExpectType ElementVNode<undefined>
h23("br", {}) // $ExpectType ElementVNode<unknown>
h24("br", {}) // $ExpectType ElementVNode<any>

0 comments on commit 3e73842

Please sign in to comment.