I am new to TypeScript and struggling to comprehend the error message below.
Type '{ order: string; }[]' is not compatible with type 'TestType[]'.
Type '{ order: string; }' is not assignable to type 'TestType'.
Types of property 'order' are conflicting.
String type cannot be assigned to number or ''.
This is the test code I have:
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 here's the TypeScript playground link
The error originates from the last line testTypes: stub
I believe there may be a mistake on my end, but I'm unsure why ''
cannot be utilized alongside number | ''
Is there a solution to this issue?