Skip to content

Commit d4ce63a

Browse files
committed
Merge pull request #336 from Shearerbeard/typescript-definitions
Typescript definitions
2 parents 00de8a9 + e1ab4f2 commit d4ce63a

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class TodoView extends Component {
119119
* [Todo](https://github.com/benstokoe/alt-todo)
120120
* [Typeahead](https://github.com/timtyrrell/alt-typeahead)
121121
* [Isomorphic React Examples](https://github.com/goatslacker/isomorphic-react-examples)
122+
* [Typescript Project](https://github.com/Shearerbeard/alt-typescript-tutorial)
122123

123124
### Boilerplates
124125

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

178179
### ES6
179180

181+
##Typescript Definitions
182+
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.
183+
184+
Using Typescript 1.5 you can import with the legacy syntax:
185+
```
186+
import Alt = require("alt");
187+
```
188+
180189
Alt is written in, and encourages ES6. It is completely optional but it is pleasant to write.
181190

182191
You can use the es6 transpiler that comes with react courtesy of

typings/alt/alt.d.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Type definitions for Alt 0.16.10
2+
// Project: https://github.com/goatslacker/alt
3+
// Definitions by: Michael Shearer <https://github.com/Shearerbeard>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/// <reference path="../flux/flux.d.ts"/>
7+
8+
9+
declare module AltJS {
10+
11+
export interface StoreModel<S> {
12+
bindActions?( ...actions:Array<Object>);
13+
exportPublicMethods?<M>(exportConfig:M):void;
14+
getState?():S;
15+
exportAsync?(source:Source);
16+
waitFor?(store:AltStore<any>):void;
17+
}
18+
19+
export type Source = {[name:string]:() => SourceModel};
20+
21+
export interface SourceModel {
22+
local( ...args:Array<any>);
23+
remote( ...args:Array<any>);
24+
shouldFetch?(state:Object, ...args:Array<any>);
25+
loading: ( ...args:Array<any>) => void;
26+
success:( ...args:Array<any>) => void;
27+
error:( ...args:Array<any>) => void;
28+
interceptResponse?(response:Object, action:AltJS.Action<any>, ...args:Array<any>);
29+
}
30+
31+
export interface AltStore<S> {
32+
getState():S;
33+
listen(handler:(state:S) => any):() => void;
34+
unlisten(handler:(state:S) => any):void;
35+
emitChange():void;
36+
}
37+
38+
export enum lifeCycleEvents {
39+
bootstrap,
40+
snapshot,
41+
init,
42+
rollback,
43+
error
44+
}
45+
46+
export type Actions = {[action:string]:Action<any>};
47+
48+
export interface Action<T> {
49+
(T);
50+
defer(data:any):void;
51+
}
52+
53+
export interface ActionsClass {
54+
generateActions?( ...action:Array<string>);
55+
dispatch( ...payload:Array<any>);
56+
actions?:Actions;
57+
}
58+
}
59+
60+
declare module "alt/utils/chromeDebug" {
61+
function chromeDebug(alt:any):void;
62+
export = chromeDebug;
63+
}
64+
65+
declare module "alt/AltContainer" {
66+
67+
import * as React from "react";
68+
69+
interface ContainerProps {
70+
store:AltJS.AltStore<any>
71+
}
72+
73+
class AltContainer extends React.Component<ContainerProps, any> {
74+
}
75+
76+
export = AltContainer;
77+
}
78+
79+
declare module "alt" {
80+
81+
import {Dispatcher} from "flux";
82+
83+
type StateTransform = (store:StoreModel<any>) => AltJS.AltStore<any>;
84+
85+
interface AltConfig {
86+
dispatcher?:Dispatcher<any>;
87+
serialize?:(data:Object) => string;
88+
deserialize?:(serialized:string) => Object;
89+
storeTransforms?:Array<StateTransform>;
90+
batchingFunction?:(callback:( ...data:Array<any>) => any) => void;
91+
}
92+
93+
class Alt {
94+
constructor(config?:AltConfig);
95+
actions:AltJS.Actions;
96+
bootstrap(data:string);
97+
takeSnapshot( ...storeNames:Array<string>):string;
98+
flush():Object;
99+
recycle( ...store:Array<AltJS.AltStore<any>>);
100+
rollback();
101+
dispatch(action?:AltJS.Action<any>, data?:Object, details?:any);
102+
addActions(actionsName:string, actions:AltJS.ActionsClass);
103+
addStore(name:string, store:StoreModel<any>, saveStore?:boolean);
104+
getStore(name:string):AltJS.AltStore<any>;
105+
getActions(actionsName:string):AltJS.Actions;
106+
createAction<T>(name:string, implementation:AltJS.ActionsClass):AltJS.Action<T>;
107+
createAction<T>(name:string, implementation:AltJS.ActionsClass, ...args:Array<any>):AltJS.Action<T>;
108+
createActions<T>(ActionsClass: ActionsClassConstructor, exportObj?: Object):T;
109+
createActions<T>(ActionsClass: ActionsClassConstructor, exportObj?: Object, ...constructorArgs:Array<any>):T;
110+
generateActions<T>( ...action:Array<string>):T;
111+
createStore<S>(store:StoreModel<S>, name?:string):AltJS.AltStore<S>;
112+
}
113+
114+
type ActionsClassConstructor = new (alt:Alt) => AltJS.ActionsClass;
115+
116+
type ActionHandler = ( ...data:Array<any>) => any;
117+
type ExportConfig = {[key:string]:( ...args:Array<any>) => any};
118+
119+
interface StoreReduce {
120+
action:any;
121+
data: any;
122+
}
123+
124+
interface StoreModel<S> extends AltJS.StoreModel<S> {
125+
setState?(currentState:Object, nextState:Object):Object;
126+
getState?():S;
127+
onSerialize?(data:any):void;
128+
onDeserialize?(data:any):void;
129+
on?(event:AltJS.lifeCycleEvents, callback:() => any):void;
130+
bindActions?(action:AltJS.Action<any>, method:ActionHandler):void;
131+
bindListeners?(config:{string: AltJS.Action<any> | AltJS.Actions});
132+
waitFor?(dispatcherSource:any):void;
133+
exportPublicMethods?(exportConfig:ExportConfig):void;
134+
getInstance?():AltJS.AltStore<S>;
135+
emitChange?():void;
136+
dispatcher?:Dispatcher<any>;
137+
alt?:Alt;
138+
displayName?:string;
139+
otherwise?(data:any, action:AltJS.Action<any>);
140+
reduce?(state:any, config:StoreReduce):Object;
141+
preventDefault?();
142+
observe?(alt:Alt):any;
143+
registerAsync?(datasource:AltJS.Source);
144+
beforeEach?(payload:Object, state:Object);
145+
afterEach?(payload:Object, state:Object);
146+
unlisten?();
147+
}
148+
149+
type StoreModelConstructor = (alt:Alt) => StoreModel<any>;
150+
151+
interface AltFactory {
152+
new(config?:AltConfig):Alt;
153+
}
154+
155+
export = Alt;
156+
}

0 commit comments

Comments
 (0)