-
Notifications
You must be signed in to change notification settings - Fork 80
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
Generalized .d.ts typedef #67
Conversation
My example. For more detailed description of actions // actions.ts
import { createActions } from 'reduxsauce';
import { Action } from 'redux';
import { DefaultFetchingStatuses } from '@app/common/constants/defaultFetchingStatuses';
export const enum TypesNames {
LOGIN_ON_SUBMIT = 'LOGIN_ON_SUBMIT',
LOGIN_CHANGE_FETCH_STATUS = 'LOGIN_CHANGE_FETCH_STATUS',
}
export interface IOnSubmit extends Action<TypesNames.LOGIN_ON_SUBMIT> {
login: string;
password: string;
}
export interface IChangeFetchStatus extends Action<TypesNames.LOGIN_CHANGE_FETCH_STATUS> {
status: DefaultFetchingStatuses;
}
export const { Types, Creators } = createActions<{
[TypesNames.LOGIN_ON_SUBMIT]: string;
[TypesNames.LOGIN_CHANGE_FETCH_STATUS]: string;
}, {
loginOnSubmit: (login: string, password: string) => IOnSubmit;
loginChangeFetchStatus: (status: DefaultFetchingStatuses) => IChangeFetchStatus;
}>({
loginOnSubmit: ['login', 'password'],
loginChangeFetchStatus: ['status'],
}); // reducer.ts
import { createReducer } from 'reduxsauce';
import { IChangeFetchStatus, IOnSubmit, Types } from './actions';
import { DefaultFetchingStatuses } from '@app/common/constants/defaultFetchingStatuses';
export interface IState {
login: string;
password: string;
fetchStatus: DefaultFetchingStatuses;
}
export const reducer = createReducer<IState, IOnSubmit | IChangeFetchStatus>({
login: null,
password: null,
fetchStatus: DefaultFetchingStatuses.NONE,
}, {
[Types.LOGIN_ON_SUBMIT]: (state, action: IOnSubmit) => ({
...state,
login: action.login,
password: action.password,
fetchStatus: DefaultFetchingStatuses.IN_PROGRESS,
}),
[Types.LOGIN_CHANGE_FETCH_STATUS]: (state, action: IChangeFetchStatus) => ({
...state,
fetchStatus: action.status,
}),
}); |
🎉 This PR is included in version 1.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
So many thanks @DiFuks ^^ You saved my day. I also find my solution with Reduxsauce/Ducks pattern
|
@emonno13 |
yeah, maybe i will use it in the future. Tks u <3 |
Hello there :)
I've been using this lib for some time now and I really enjoy it, didn't know it had typescript definitions tho since I've been using it at work and our codebase is js only, no ts... Anyway, when I saw the current typedef I thought it didn't let the developer be as thorough with their definitions as they may want to be so I made a few additions... I think someone proposed something similar to this PR in a question as well...
An example of how to use the new typedef:
The main idea was to keep the old definition as default while allowing the developer to provide more granular definitions for their specific use case. I tested this a bit and was quite pleased, I hope you will be too. I did try to eliminate the need to declare the action types as a separate interface but it does not seem to be possible to just grab some keys from a definition, map them to screaming snake case and then use them in another definition...
Hope to hear some feedback soon!