typescript2.4 - TypeScript 2.4 Generic Inference causing compilation errors -
i seeing existing typescript code break due changes in generic inference.
example:
interface action { type: string; } interface myaction extends action { payload: string; } interface mystate {} type reducer<s> = <a extends action>(state: s, action: a) => s; const myreducer: reducer<mystate> = (state: mystate, action: myaction) => { if (action.type === "myactiontype") { return {}; } else { return {}; } };
and compilation error:
error:(11, 7) ts2322:type '(state: mystate, action: myaction) => {}' not assignable type 'reducer<mystate>'. types of parameters 'action' , 'action' incompatible. type 'a' not assignable type 'myaction'. type 'action' not assignable type 'myaction'. property 'payload' missing in type 'action'.
interface myotheraction { type: 'myotheractiontype' foo: number } declare const state: state declare const myotheraction: myotheraction // following valid call according myreducer's signature myreducer(state, myotheraction)
however value have assigned myreducer
doesn't accept every type of action, therefore error.
there's no reason make second parameter generic, aren't creating constraint between 2 parameters/return value. do
type reducer<s> = (state: s, action: action) => s;
Comments
Post a Comment