Skip to content
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

Use void instead of any for undefined payloads #174

Merged
merged 2 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IsUnknownOrNonInferrable } from './tsHelpers'
* @template M The type of the action's meta (optional)
*/
export type PayloadAction<
P = any,
P = void,
T extends string = string,
M = void
> = WithOptionalMeta<M, WithPayload<P, Action<T>>>
Expand Down Expand Up @@ -62,7 +62,7 @@ export type ActionCreatorWithPayload<
* An action creator that produces actions with a `payload` attribute.
*/
export type PayloadActionCreator<
P = any,
P = void,
T extends string = string,
PA extends PrepareAction<P> | void = void
> = IfPrepareActionMethodProvided<
Expand Down Expand Up @@ -94,7 +94,7 @@ export type PayloadActionCreator<
* If this is given, the resulting action creator will pass it's arguments to this method to calculate payload & meta.
*/

export function createAction<P = any, T extends string = string>(
export function createAction<P = void, T extends string = string>(
type: T
): PayloadActionCreator<P, T>

Expand Down
21 changes: 10 additions & 11 deletions type-tests/files/createAction.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ function expectType<T>(p: T): T {
}

/*
* Test: PayloadAction type parameter is optional (defaults to `any`).
* Test: PayloadAction type parameter is required.
*/
{
// typings:expect-error
const action: PayloadAction = { type: '', payload: 5 }
// typings:expect-error
const numberPayload: number = action.payload
// typings:expect-error
const stringPayload: string = action.payload
}

/*
* Test: PayloadAction has a string type tag.
*/
{
const action: PayloadAction = { type: '', payload: 5 }
const action: PayloadAction<number> = { type: '', payload: 5 }

// typings:expect-error
const action2: PayloadAction = { type: 1, payload: 5 }
Expand All @@ -41,7 +44,7 @@ function expectType<T>(p: T): T {
* Test: PayloadAction is compatible with Action<string>
*/
{
const action: PayloadAction = { type: '', payload: 5 }
const action: PayloadAction<number> = { type: '', payload: 5 }
const stringAction: Action<string> = action
}

Expand All @@ -58,7 +61,7 @@ function expectType<T>(p: T): T {
payload
}),
{ type: 'action' }
) as PayloadActionCreator
) as PayloadActionCreator<number | undefined>

expectType<PayloadAction<number>>(actionCreator(1))
expectType<PayloadAction<undefined>>(actionCreator())
Expand Down Expand Up @@ -110,22 +113,18 @@ function expectType<T>(p: T): T {
}

/*
* Test: createAction() type parameter is optional (defaults to `any`).
* Test: createAction() type parameter is required, not inferred (defaults to `void`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test doesn't really make any more sense (description does not match what it tests right now), so I would delete it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, lemme rewrite this one

*/
{
const increment = createAction('increment')
const n: number = increment(1).payload
const s: string = increment('1').payload

// but infers the payload type to be the argument type
// typings:expect-error
const t: string = increment(1).payload
const n: number = increment(1).payload
}
/*
* Test: createAction().type is a string literal.
*/
{
const increment = createAction('increment')
const increment = createAction<number, 'increment'>('increment')
const n: string = increment(1).type
const s: 'increment' = increment(1).type

Expand Down