我有一个 redux 风格的 reducer(我使用的是 ngrx),它返回一个特定的类型。当我在返回对象中使用扩展运算符时,打字稿 linter 没有捕获无效属性。
这是我的界面:
interface MyState {
firstName: string;
lastName: string;
age: number;
}
Run Code Online (Sandbox Code Playgroud)
这是我的减速机。Action是一个 ngrx 动作:
function reducer(state = initialState, action: Action): MyState {
switch (action.type) {
case Actions.COOL_ACTION:
return {
...state,
propertyDoesNotExist: action.payload, // <- no error
};
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这propertyDoesNotExist会被标记,但事实并非如此。我试过转换<CalendarState>返回对象、状态属性...(<CalendarState>state)并使用as别名,但它没有帮助。
这就像扩展运算符弄乱了类型。
我想编写一个函数,使我返回包裹在另一个组件中的组件。我尝试编写的功能类似于下面的JavaScript。
function GetGroup({ name, text, isRequired, ...props })
Run Code Online (Sandbox Code Playgroud)
这里,name,text,和isRequired从传递的参数获得的和其他被送到另一个部件如props。
如何用TypeScript编写它?