-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript definition #206
Comments
Would #254 help here, or is “definition” a separate thing? |
TS definition files are different from Flow definition files and AFAIK they aren't compatible with each other |
They are a bit different. declare module "redux" {
interface ActionCreator {
(...args: Array<any>): Object;
}
interface ActionCreators {
[key: string]: ActionCreator
}
interface Reducer {
(state: any, action: Object): any;
}
interface StoreMethods {
dispatch(action: Object): Object;
getState(): Object;
}
class Store {
getReducer(): Function;
replaceReducer(nextReducer: Reducer): void;
dispatch(action: Object): Object;
getState(): Object;
subscribe(listener: Function): Function;
}
function createStore(
reducer: Reducer | Object,
initialState?: any
): Store;
function bindActionCreators<T>(
actionCreators: ActionCreator | ActionCreators,
dispatch: Function
): T;
function composeMiddleware(...middlewares: Array<Function>): Function;
function composeReducers(reducers: Array<Reducer>): Reducer;
}
declare module "redux/react" {
import React = require("react");
import redux = require("redux");
interface ProviderProps {
store: redux.Store;
children: Function;
}
interface ProviderState {
store: redux.Store;
}
class Provider extends React.Component<ProviderProps, ProviderState> {}
interface ConnectorProps {
children: Function;
select: Function;
}
class Connector extends React.Component<ConnectorProps, any> {}
} It's missing some stuff stilll but it's getting there |
Updated a bit (and will likely wait for redux/react-redux to hit 1.0): declare module "redux" {
interface ActionCreator {
(...args: Array<any>): Object;
}
interface ActionCreators {
[key: string]: ActionCreator
}
interface Reducer {
(state: any, action: Object): any;
}
interface StoreMethods {
dispatch(action: Object): Object;
getState(): Object;
}
class Store {
getReducer(): Function;
replaceReducer(nextReducer: Reducer): void;
dispatch(action: Object): Object;
getState(): Object;
subscribe(listener: Function): Function;
}
function createStore(
reducer: Reducer | Object,
initialState?: any
): Store;
function bindActionCreators<T>(
actionCreators: ActionCreator | ActionCreators,
dispatch: Function
): T;
function composeMiddleware(...middlewares: Array<Function>): Function;
function combineReducers(reducers: Object): Reducer;
}
declare module "react-redux" {
import React = require("react");
import redux = require("redux");
interface ProviderProps {
store: redux.Store;
children: Function;
}
interface ProviderState {
store: redux.Store;
}
class Provider extends React.Component<ProviderProps, ProviderState> {}
interface ConnectorProps {
children: Function;
select: Function;
}
class Connector extends React.Component<ConnectorProps, any> {}
} |
Nice job!
|
That's how it is in 1.0 RC (and will be in 1.0). The React bindings are split into http://github.com/gaearon/react-redux. |
It should return any indeed |
Here is a slightly updated version (without react-redux). declare module Redux {
interface ActionCreator extends Function {
(...args: any[]): any;
}
interface ActionCreators {
[key: string]: ActionCreator
}
interface Reducer extends Function {
(state: any, action: any): any;
}
interface Dispatch extends Function {
(action: any): any;
}
interface StoreMethods {
dispatch: Dispatch;
getState(): any;
}
interface MiddlewareArg {
dispatch: Dispatch;
getState: Function;
}
interface Middleware extends Function {
(obj: MiddlewareArg): Function;
}
class Store {
getReducer(): Reducer;
replaceReducer(nextReducer: Reducer): void;
dispatch(action: any): any;
getState(): any;
subscribe(listener: Function): Function;
}
function createStore(reducer: Reducer, initialState?: any): Store;
function bindActionCreators<T>(actionCreators: ActionCreator | ActionCreators, dispatch: Dispatch): T;
function composeMiddleware(...middlewares: any[]): Function;
function combineReducers(reducers: any): Reducer;
function applyMiddleware(...middleware: Middleware[]): Function;
}
declare module "redux" {
export = Redux;
}; |
@wbuchwalter This looks good to me! One minor change is I don't think It also would be nice to describe |
Looks good ! I would wait until 1.0 to submit it on definitelytyped. |
@Keats |
Yes indeed, I realised after posting, as always. Cheers |
Updated following your suggestions: declare module Redux {
interface ActionCreator extends Function {
(...args: any[]): any;
}
interface ActionCreators {
[key: string]: ActionCreator;
}
interface Reducer extends Function {
(state: any, action: any): any;
}
interface Reducers {
[key: string]: Reducer;
}
interface Dispatch extends Function {
(action: any): any;
}
interface StoreMethods {
dispatch: Dispatch;
getState(): any;
}
interface MiddlewareArg {
dispatch: Dispatch;
getState: Function;
}
interface Middleware extends Function {
(obj: MiddlewareArg): Function;
}
class Store {
getReducer(): Reducer;
replaceReducer(nextReducer: Reducer): void;
dispatch(action: any): any;
getState(): any;
subscribe(listener: Function): Function;
}
function createStore(reducer: Reducer, initialState?: any): Store;
function bindActionCreators<T>(actionCreators: ActionCreators, dispatch: Dispatch): T;
function combineReducers(reducers: Reducers): Reducer;
function applyMiddleware(...middleware: Middleware[]): Function;
}
declare module "redux" {
export = Redux;
} @Keats Regarding DefinitlyTyped, we can add a README and a prefix to make it clear this is targeting 1.0 if that is your concern. |
Minor change: I just merged a PR that also lets |
Seems that Typescript isn't able to understand that when doing |
@wbuchwalter Yeah Typescript can't guess the type from import * as reducers so you could cast it, which is i think a better solution than removing the types |
The thing is that you could cast the |
Why are you using interface Reducer<S, A> {
(state: S, action: A): S;
} |
Same as #299, you can change Also in the case of CreateStore(reducer: Reducer<any, any>, initialState: any?);
getReducer(): Reducer<any, any>; And on a side note if interface Action<T> {
type: string;
payload: T;
} But it's kind of tricky since everyone seems to use his own kind of actions, so which Action structure should we use? there is Flux Standard Action which i think is great but I don't know how much people folllow that and if it should be enforced. TL;DR: I'm using any because of laziness 😄 |
I've been working on the Flow types for redux here: #356, perhaps that will be helpful since I'm using generics. Some function apis, like |
Since 1.0.0 is released, there is now a PR on DefinitelyTyped, so I guess we can move our discussion there and close this issue? |
Sounds good. |
We're a few version down the road any have an updated Typescript definition? I've looked around but only found the 1.0.0 version. |
Is this now on DefinitelyType? Can't find it... Or where can we access the latest version of your attempts? Just above or...? |
thx! I obviously missed it... |
Please help review the new official typings in #1413. |
@leoasis I am not sure whether the issue you mention above about typing combineReducers is exactly what I'm thinking or whether it's still relevant, but I recently found this solution with the keyof feature in TypeScript 2.1: microsoft/TypeScript#1213 (comment) This allows us to create a type-safe (relative to the state type) combineReducers function. Edit: Created an issue here: #2238 |
Just an issue to remind me to start doing it as soon as #195 is merged.
Normally that would live in https://github.com/borisyankov/DefinitelyTyped but it doesn't support typescript 1.5 yet so I'll put it here if that's ok, it's also a good way to quickly see the public api of a library imo
The text was updated successfully, but these errors were encountered: