Skip to content

Commit

Permalink
Type improvements (#706)
Browse files Browse the repository at this point in the history
* use nanobus for the emitter typings

instead of `events`, which is a bit different

* add typings for `cache` option
  • Loading branch information
goto-bus-stop authored Jan 23, 2020
1 parent 2d56f30 commit 2893de8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
14 changes: 9 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/// <reference types="node" />

import * as EventEmitter from 'events'
import Nanobus = require('nanobus')

export = Choo

declare class Choo {
constructor (opts?: Choo.IChoo)
use (callback: (state: Choo.IState, emitter: EventEmitter, app: this) => void): void
route (routeName: string, handler: (state: Choo.IState, emit: (name: string, ...args: any[]) => void) => void): void
use (callback: (state: Choo.IState, emitter: Nanobus, app: this) => void): void
route (routeName: string, handler: (state: Choo.IState, emit: Nanobus['emit']) => void): void
mount (selector: string): void
start (): HTMLElement
toString (location: string, state?: Choo.IState): string
Expand All @@ -18,6 +16,12 @@ declare namespace Choo {
history?: boolean
href?: boolean
hash?: boolean
cache?: number | ICache
}

export interface ICache {
get(id: string | number): undefined | null | any
set(id: string | number, element: any): void
}

export interface IState {
Expand Down
40 changes: 40 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expectAssignable, expectType } from 'tsd'
import Choo = require('.')

new Choo({})
new Choo()

new Choo({ cache: 100 })
new Choo({
cache: new Map()
})
new Choo({
cache: {
get: (id) => null,
set: (id, value) => expectType<any>(value)
}
})

const app = new Choo({
history: false,
href: true,
})

app.use((state, emitter) => {
state.title = 'choo choo'
emitter.on(state.events.DOMCONTENTLOADED, () => {
emitter.emit('example')
})
})

app.route('/', (state, emit) => {
expectAssignable<object>(state.params)
expectType<string>(state.href)
expectType<string>(state.route)
expectType<string>(state.title)
emit('example')
})

expectType<string>(app.toString('/'))

app.mount('body')
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"inspect": "browserify --full-paths index -p tinyify | discify --open",
"prepublishOnly": "npm run build",
"start": "bankai start example",
"test": "standard && npm run deps && npm run test:node && npm run test:browser",
"test": "standard && npm run deps && npm run test:types && npm run test:node && npm run test:browser",
"test:types": "tsd",
"test:node": "node test/node.js | tap-format-spec",
"test:browser": "browserify test/browser.js | tape-run | tap-format-spec"
},
Expand All @@ -40,7 +41,7 @@
"dependencies": {
"document-ready": "^2.0.1",
"nanoassert": "^1.1.0",
"nanobus": "^4.2.0",
"nanobus": "^4.4.0",
"nanocomponent": "^6.5.0",
"nanohref": "^3.0.0",
"nanohtml": "^1.1.0",
Expand All @@ -54,7 +55,6 @@
},
"devDependencies": {
"@tap-format/spec": "^0.2.0",
"@types/node": "^10.3.1",
"browserify": "^16.2.2",
"bundle-collapser": "^1.2.1",
"dependency-check": "^3.1.0",
Expand All @@ -65,6 +65,14 @@
"standard": "^11.0.1",
"tape": "^4.6.3",
"tape-run": "^6.0.0",
"tinyify": "^2.2.0"
"tinyify": "^2.2.0",
"tsd": "^0.11.0"
},
"tsd": {
"compilerOptions": {
"lib": [
"DOM"
]
}
}
}

0 comments on commit 2893de8

Please sign in to comment.