I'm new to TypeScript and I'm having a hard time understanding the following error.
Type '{ order: string; }[]' is not assignable to type 'TestType[]'.
Type '{ order: string; }' is not assignable to type 'TestType'.
Types of property 'order' are incompatible.
Type 'string' is not assignable to type 'number | ""'.
This is my test code
export enum ACTION_TYPES {
TEST_ACTION = 'TEST_ACTION',
}
export type TestType = {
order: number | '';
};
export function TestAction(
testTypes: TestType[]
): {
type: ACTION_TYPES.TEST_ACTION;
testTypes: TestType[];
} {
return {
type: ACTION_TYPES.TEST_ACTION,
testTypes,
};
}
export type PluginsState = {
testTypes: TestType[];
};
export type Actions =
| ReturnType< typeof TestAction >;
const reducer = (
state: PluginsState = {
testTypes: [],
},
payload?: Actions
): PluginsState => {
if ( payload && 'type' in payload ) {
switch ( payload.type ) {
case ACTION_TYPES.TEST_ACTION:
return {
...state,
testTypes: payload.testTypes,
};
}
}
return state;
};
export const stub = [
{
order: '',
}
];
const defaultState: PluginsState = {
testTypes: [],
};
reducer( defaultState, {
type: ACTION_TYPES.TEST_ACTION,
testTypes: stub,
} );
and this is TypeScript playground link
The error comes from the last line testTypes: stub
I'm pretty sure there is something I'm doing wrong, but I'm not understanding why '' cannot be used with number | ""
Is there a workaround to this error?