TypeScript错误:Type';字符串';不可分配给类型';编号|”&引用';
我是TypeScript新手,很难理解下面的错误TypeScript错误:Type';字符串';不可分配给类型';编号|”&引用';,typescript,Typescript,我是TypeScript新手,很难理解下面的错误 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 ty
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 | ""'.
这是我的测试代码
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,
} );
导出枚举操作类型{
测试动作=‘测试动作’,
}
导出类型TestType={
顺序:编号|“”;
};
导出函数测试(
testTypes:TestType[]
): {
类型:动作类型。测试动作;
testTypes:TestType[];
} {
返回{
类型:动作类型。测试动作,
测试类型,
};
}
导出类型pluginState={
testTypes:TestType[];
};
导出类型操作=
|返回类型<测试类型>;
常数减速机=(
状态:pluginState={
测试类型:[],
},
有效载荷?:行动
):pluginstate=>{
if(有效载荷和有效载荷中的“类型”){
开关(有效负载类型){
案例操作类型。测试操作:
返回{
……国家,
testTypes:payload.testTypes,
};
}
}
返回状态;
};
导出常量存根=[
{
命令:“”,
}
];
const defaultState:pluginstate={
测试类型:[],
};
减速器(默认状态{
类型:动作类型。测试动作,
测试类型:存根,
} );
这是打字游戏
错误来自最后一行testTypes:stub
我很确定我做错了什么,但我不明白为什么'
不能与number|'
是否有解决此错误的方法?从代码作者的角度来看,您所写的内容是正确的,应该可以正常工作。但是,这里是Typescript如何看待它(而是推断它):
导出常量存根=[
{
命令:“”,
}
];
这本质上是一种类型:{order:string}[]
,在大多数情况下都有意义。但是,这是您的{order:'}[]
的通用版本,因此,Typescript不能保证传递的任何内容都是{order:number |'''}
,您可以对其进行变异,Typescript仍必须允许它。所以,它警告你
为了避免这种推断,您可以做两件事:
stub
作为TestType
,以便TypeScript在不再是该类型时出错,如下所示:
export const stub:TestType[]=[
{
命令:“,
},
] ;
导出常量存根=[
{
命令:“,
}作为康斯特,
] ;
export const stub = [
{
order: ''
}
];
编译器必须推断存根的类型。它可以有各种类型(例如或Array
等)。因此编译器使用启发式规则来选择它认为最合适的。在上述情况下,推断的类型为:
/* const stub: Array<{order: string}> */
但不幸的是,这不是你想要的类型。当您稍后尝试在中使用它作为传递给reducer()
的参数的一部分时,编译器将{order:string}
视为不可分配给TestType
,您将得到一个错误
为了解决此问题,您需要将存根的类型显式地设置为所需的类型:
export const stub: Array<TestType> = [ // annotated
{ order: '' }
];
reducer(defaultState, {
type: ACTION_TYPES.TEST_ACTION,
testTypes: stub, // okay
});
非常感谢(再次)您的详细回答!
export const stub: Array<TestType> = [ // annotated
{ order: '' }
];
reducer(defaultState, {
type: ACTION_TYPES.TEST_ACTION,
testTypes: stub, // okay
});
export const stub = [
{
order: '' as const, // const assertion
}
];
/* const stub: Array<{ order: ""; } */
reducer(defaultState, {
type: ACTION_TYPES.TEST_ACTION,
testTypes: stub,
});