Skip to content

Commit

Permalink
updated: improved typescript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed May 8, 2021
1 parent b911497 commit 3cee028
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions riot.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlotBindingData, TemplateChunk, AttributeExpressionData, ExpressionType, BindingType } from '@riotjs/dom-bindings'
import { SlotBindingData, TemplateChunk, AttributeExpressionData, ExpressionType, BindingType, template } from '@riotjs/dom-bindings'

// This interface is only exposed and any Riot component will receive the following properties
export interface RiotCoreComponent<Props = object, State = object> {
export interface RiotCoreComponent<Props extends object, State extends object> {
// automatically generated on any component instance
readonly props: Props
readonly root: HTMLElement
Expand All @@ -23,8 +23,12 @@ export interface RiotCoreComponent<Props = object, State = object> {
$$(selector: string): [HTMLElement]
}

export type RiotComponentsMap = {
[key: string]: RiotComponentShell<any, any>
}

// Riot Pure Component interface that should be used together with riot.pure
export interface RiotPureComponent<Context = object> {
export interface RiotPureComponent<Context extends object> {
mount(
element: HTMLElement,
context?: Context,
Expand All @@ -35,27 +39,25 @@ export interface RiotPureComponent<Context = object> {
unmount(keepRootElement: boolean): RiotPureComponent<Context>
}

export interface PureComponentFactoryFunction<InitialProps = any, Context = object> {
export interface PureComponentFactoryFunction<InitialProps extends object, Context extends object> {
({slots, attributes, props}:{ slots?: SlotBindingData[], attributes?: AttributeExpressionData[], props?: InitialProps; }): RiotPureComponent<Context>
}

// This object interface is created anytime a riot file will be compiled into javascript
export interface RiotComponentShell<Props = object, State = object> {
export interface RiotComponentShell<Props extends object, State extends object> {
readonly css?: string
readonly exports?: (() => RiotComponentExport<Props, State>)|RiotComponentExport<Props, State>
readonly name?: string
template(template: Function, expressionTypes: ExpressionType, bindingTypes: BindingType, getComponent: (componentName: string) => any): TemplateChunk
template(templateFn: typeof template, expressionTypes: ExpressionType, bindingTypes: BindingType, getComponent: (componentName: string) => any): TemplateChunk
}

// Interface that can be used when creating the components export
export interface RiotComponentExport<Props = object, State = object> {
export interface RiotComponentExport<Props extends object, State extends object> {
// optional on the component object
state?: State

// optional alias to map the children component names
components?: {
[key: string]: RiotComponentShell<Props, State>
}
components?: RiotComponentsMap

// state handling methods
shouldUpdate?(newProps: Props, currentProps: Props): boolean
Expand All @@ -71,23 +73,23 @@ export interface RiotComponentExport<Props = object, State = object> {
}

// All the RiotComponent Public interface properties are optional
export interface RiotComponent<Props = object, State = object> extends RiotCoreComponent<Props, State>, RiotComponentExport<Props, State> {}
export interface RiotComponent<Props extends object, State extends object> extends RiotCoreComponent<Props, State>, RiotComponentExport<Props, State> {}

export type RegisteredComponentsMap = Map<string, () => RiotComponent>
export type ComponentEnhancer = <Props, State>(component: RiotComponent<Props, State>) => RiotComponent<Props, State>
export type RegisteredComponentsMap = Map<string, () => RiotComponent<any, any>>
export type ComponentEnhancer = <Props extends object, State extends object>(component: RiotComponent<Props, State>) => RiotComponent<Props, State>
export type InstalledPluginsSet = Set<ComponentEnhancer>

export function register<Props, State>(componentName: string, shell: RiotComponentShell<Props, State>): RegisteredComponentsMap
export function register<Props extends object, State extends object>(componentName: string, shell: RiotComponentShell<Props, State>): RegisteredComponentsMap
export function unregister(componentName: string): RegisteredComponentsMap
export function mount<Props = object, State = object>(selector: string|HTMLElement, initialProps?: Props, componentName?: string): RiotComponent<Props, State>[]
export function mount<Props extends object, State extends object>(selector: string|HTMLElement, initialProps?: Props, componentName?: string): RiotComponent<Props, State>[]
export function unmount(selector: string|HTMLElement, keepRootElement: boolean):HTMLElement[]
export function install(plugin: ComponentEnhancer):InstalledPluginsSet
export function uninstall(plugin: ComponentEnhancer):InstalledPluginsSet
export function component<Props , State>(shell: RiotComponentShell<Props, State>):(
export function component<Props extends object, State extends object>(shell: RiotComponentShell<Props, State>):(
el: HTMLElement,
initialProps?: Props,
meta?: { slots: SlotBindingData[]; attributes: AttributeExpressionData[]; parentScope: any; }
) => RiotComponent<Props, State>

export function pure<InitialProps = object, Context = object, FactoryFunction = PureComponentFactoryFunction<InitialProps, Context>>(func: FactoryFunction): FactoryFunction
export function pure<InitialProps extends object, Context extends object, FactoryFunction = PureComponentFactoryFunction<InitialProps, Context>>(func: FactoryFunction): FactoryFunction
export const version: string

0 comments on commit 3cee028

Please sign in to comment.