Skip to content

Commit

Permalink
added: typescript support #2707
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Jun 7, 2019
1 parent 15d5bf3 commit 1198c23
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@
"files": [
"src",
"riot.js",
"riot.d.ts",
"riot.esm.js",
"riot.min.js",
"riot+compiler.js",
"riot+compiler.min.js"
],
"main": "riot.js",
"module": "riot.esm.js",
"jsnext:main": "riot.esm.js"
"jsnext:main": "riot.esm.js",
"types": "./riot.d.ts",
}
67 changes: 67 additions & 0 deletions riot.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This interface is only exposed and any Riot component will receive the following properties
export interface RiotCoreComponent {
// automatically generated on any component instance
props: object
root: HTMLElement
name?: string
slots: any[]
mount(
element: HTMLElement,
initialState?: object,
parentScope?: object
): RiotComponent
update(
newState?: object,
parentScope?: object
): RiotComponent
unmount(keepRootElement: boolean): RiotComponent

// Helpers
$(selector: string): HTMLElement
$$(selector: string): [HTMLElement]
}

export interface RiotComponentShell {
css?: string
exports?: any
name?: string
// TODO: add the @riotjs/dom-bindings types
template(): any
}

// All the RiotComponent Public interface properties are optional
export interface RiotComponent extends RiotCoreComponent {
// optional on the component object
state?: object

// optional alias to map the children component names
components?: {
[key: string]: RiotComponentShell
}

// state handling methods
shouldUpdate?(newProps: object, currentProps: object): boolean

// lifecycle methods
onBeforeMount?(currentProps: object, currentState: object): void
onMounted?(currentProps: object, currentState: object): void
onBeforeUpdate?(currentProps: object, currentState: object): void
onUpdated?(currentProps: object, currentState: object): void
onBeforeUnmount?(currentProps: object, currentState: object): void
onUnmounted?(currentProps: object, currentState: object): void
}

export type RegisteredComponentsMap = Map<string, () => RiotComponent>
export type ComponentEnhancer = (component: RiotComponent) => RiotComponent
export type InstalledPluginsSet = Set<ComponentEnhancer>

declare module 'riot' {
export function register(componentName: string, shell: RiotComponentShell): RegisteredComponentsMap
export function unregister(componentName: string): RegisteredComponentsMap
export function mount(selector: string, componentName: string, initialProps: object): RiotComponent[]
export function unmount(selector: string):HTMLElement[]
export function install(plugin: ComponentEnhancer):InstalledPluginsSet
export function uninstall(plugin: ComponentEnhancer):InstalledPluginsSet
export function version: string
export function component(shell: RiotComponentShell):(el: HTMLElement, initialProps?: object) => RiotComponent
}

0 comments on commit 1198c23

Please sign in to comment.