-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
TypeScript Version:
2.1.5 and nightly (2.2.0-dev.20170125)
Code
// actions.ts
export interface FluxStandardAction<Payload, Meta> {
type: string,
payload: Payload,
meta: Meta
}
export function createActionCreator<Payload, Meta>(type: string)
: (payload?: Payload, meta?: Meta) => FluxStandardAction<Payload, Meta> {
return (payload: Payload, meta: any) => {
return createAction(type, payload, meta)
}
}
export function createAction<Payload, Meta>(type: string, payload?: Payload, meta?: Meta)
: FluxStandardAction<Payload, Meta> {
return { type, payload, meta }
}
// consume.ts
import { FluxStandardAction, createActionCreator } from './actions'
interface AuthenticatePayload {
authenticating: boolean
authenticated: boolean,
username?: string,
errorMessage?: string
}
export const authenticate = createActionCreator<AuthenticatePayload, void>(AuthenticateActionType)
// type of `authenticate()`:
// (payload?: AuthenticatePayload | undefined, meta?: void | undefined)
// => FluxStandardAction<AuthenticatePayload, void>I would like to enable --noUnusedLocals but the "export types can't be named" issue keep preventing me from doing so.
Above is a simple scenario that involved a higher order function createActionCreator().
The logic is pretty simple and they function it returns is not hard to understand either.
If I turn on --noUnusedLocals, the consume.ts will fail because FluxStandardAction is not explicitly used.
But if I explicitly define the type of const authenticate, it is very redundant.
On one hand, the author of createActionCreator() may create a type for the return value to fix this:
export type ActionCreator = <Payload, Meta>(payload?: Payload, meta?: Meta)
=> FluxStandardAction<Payload, Meta>
// consume.ts
import { createActionCreator, ActionCreator } from './actions'
...
const authenticate: ActionCreator
= createActionCreator<AuthenticatePayload, void>(AuthenticateActionType)However, not all package authors would do that, leaving the consumer with very verbose code with not much value gain, IMO.
As an alternative, maybe we can have an inline comment to disable checks for that particular import?
import {
// ts:disable-next-line noUnusedLocals
FluxStandardAction,
... } from './actions'
...