Skip to content

Commit

Permalink
Merge pull request #336 from Shearerbeard/typescript-definitions
Browse files Browse the repository at this point in the history
Typescript definitions
  • Loading branch information
goatslacker committed Jun 22, 2015
2 parents 00de8a9 + e1ab4f2 commit d4ce63a
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class TodoView extends Component {
* [Todo](https://github.com/benstokoe/alt-todo)
* [Typeahead](https://github.com/timtyrrell/alt-typeahead)
* [Isomorphic React Examples](https://github.com/goatslacker/isomorphic-react-examples)
* [Typescript Project](https://github.com/Shearerbeard/alt-typescript-tutorial)

### Boilerplates

Expand Down Expand Up @@ -177,6 +178,14 @@ const alt = new Alt();

### ES6

##Typescript Definitions
The typescript definitions for alt are located in the typings directory. This should be included in your project under typings/alt or whatever folder you use to manage your definitions files. You can import the dependencies react and flux, easily with [TSD](https://github.com/DefinitelyTyped/tsd). From here you can reference your typings as per usual with a reference tag ```<reference path="<path>.d.ts" />```. Check the [alt-typescript-tutorial](https://github.com/Shearerbeard/alt-typescript-tutorial) for more information and project examples.

Using Typescript 1.5 you can import with the legacy syntax:
```
import Alt = require("alt");
```

Alt is written in, and encourages ES6. It is completely optional but it is pleasant to write.

You can use the es6 transpiler that comes with react courtesy of
Expand Down
156 changes: 156 additions & 0 deletions typings/alt/alt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Type definitions for Alt 0.16.10
// Project: https://github.com/goatslacker/alt
// Definitions by: Michael Shearer <https://github.com/Shearerbeard>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

/// <reference path="../flux/flux.d.ts"/>


declare module AltJS {

export interface StoreModel<S> {
bindActions?( ...actions:Array<Object>);
exportPublicMethods?<M>(exportConfig:M):void;
getState?():S;
exportAsync?(source:Source);
waitFor?(store:AltStore<any>):void;
}

export type Source = {[name:string]:() => SourceModel};

export interface SourceModel {
local( ...args:Array<any>);
remote( ...args:Array<any>);
shouldFetch?(state:Object, ...args:Array<any>);
loading: ( ...args:Array<any>) => void;
success:( ...args:Array<any>) => void;
error:( ...args:Array<any>) => void;
interceptResponse?(response:Object, action:AltJS.Action<any>, ...args:Array<any>);
}

export interface AltStore<S> {
getState():S;
listen(handler:(state:S) => any):() => void;
unlisten(handler:(state:S) => any):void;
emitChange():void;
}

export enum lifeCycleEvents {
bootstrap,
snapshot,
init,
rollback,
error
}

export type Actions = {[action:string]:Action<any>};

export interface Action<T> {
(T);
defer(data:any):void;
}

export interface ActionsClass {
generateActions?( ...action:Array<string>);
dispatch( ...payload:Array<any>);
actions?:Actions;
}
}

declare module "alt/utils/chromeDebug" {
function chromeDebug(alt:any):void;
export = chromeDebug;
}

declare module "alt/AltContainer" {

import * as React from "react";

interface ContainerProps {
store:AltJS.AltStore<any>
}

class AltContainer extends React.Component<ContainerProps, any> {
}

export = AltContainer;
}

declare module "alt" {

import {Dispatcher} from "flux";

type StateTransform = (store:StoreModel<any>) => AltJS.AltStore<any>;

interface AltConfig {
dispatcher?:Dispatcher<any>;
serialize?:(data:Object) => string;
deserialize?:(serialized:string) => Object;
storeTransforms?:Array<StateTransform>;
batchingFunction?:(callback:( ...data:Array<any>) => any) => void;
}

class Alt {
constructor(config?:AltConfig);
actions:AltJS.Actions;
bootstrap(data:string);
takeSnapshot( ...storeNames:Array<string>):string;
flush():Object;
recycle( ...store:Array<AltJS.AltStore<any>>);
rollback();
dispatch(action?:AltJS.Action<any>, data?:Object, details?:any);
addActions(actionsName:string, actions:AltJS.ActionsClass);
addStore(name:string, store:StoreModel<any>, saveStore?:boolean);
getStore(name:string):AltJS.AltStore<any>;
getActions(actionsName:string):AltJS.Actions;
createAction<T>(name:string, implementation:AltJS.ActionsClass):AltJS.Action<T>;
createAction<T>(name:string, implementation:AltJS.ActionsClass, ...args:Array<any>):AltJS.Action<T>;
createActions<T>(ActionsClass: ActionsClassConstructor, exportObj?: Object):T;
createActions<T>(ActionsClass: ActionsClassConstructor, exportObj?: Object, ...constructorArgs:Array<any>):T;
generateActions<T>( ...action:Array<string>):T;
createStore<S>(store:StoreModel<S>, name?:string):AltJS.AltStore<S>;
}

type ActionsClassConstructor = new (alt:Alt) => AltJS.ActionsClass;

type ActionHandler = ( ...data:Array<any>) => any;
type ExportConfig = {[key:string]:( ...args:Array<any>) => any};

interface StoreReduce {
action:any;
data: any;
}

interface StoreModel<S> extends AltJS.StoreModel<S> {
setState?(currentState:Object, nextState:Object):Object;
getState?():S;
onSerialize?(data:any):void;
onDeserialize?(data:any):void;
on?(event:AltJS.lifeCycleEvents, callback:() => any):void;
bindActions?(action:AltJS.Action<any>, method:ActionHandler):void;
bindListeners?(config:{string: AltJS.Action<any> | AltJS.Actions});
waitFor?(dispatcherSource:any):void;
exportPublicMethods?(exportConfig:ExportConfig):void;
getInstance?():AltJS.AltStore<S>;
emitChange?():void;
dispatcher?:Dispatcher<any>;
alt?:Alt;
displayName?:string;
otherwise?(data:any, action:AltJS.Action<any>);
reduce?(state:any, config:StoreReduce):Object;
preventDefault?();
observe?(alt:Alt):any;
registerAsync?(datasource:AltJS.Source);
beforeEach?(payload:Object, state:Object);
afterEach?(payload:Object, state:Object);
unlisten?();
}

type StoreModelConstructor = (alt:Alt) => StoreModel<any>;

interface AltFactory {
new(config?:AltConfig):Alt;
}

export = Alt;
}

0 comments on commit d4ce63a

Please sign in to comment.