我正在学习打字稿,我用
创建了一个小应用程序create-react-app my-app --template redux-typescript
这已经创建了一个钩子来输入 useSelector
import { TypedUseSelectorHook,useSelector } from 'react-redux';
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
使用 useAppSelector 我想用参数从商店中选择一些东西
const favouriteList = useAppSelector((listKey) => selectFavouriteList(listKey));
如果没有 Typescript,我想我可以用柯里化来解决这个问题。
const selectFavouriteList = (state: RootState) => (listKey: string) => {
return state.favourites.lists[listKey]
}
但是 TypesScript 返回这个错误,它期望 store 作为参数。
Argument of type 'DefaultRootState' is not assignable to parameter of type '{ favourites: favouriteListState; }'.
Property 'favourites' is missing in type 'DefaultRootState' but required in type '{ favourites: favouriteListState; }'.ts(2345)
store.ts(8, 9): 'favourites' is declared here.
有没有人有建议如何做这个:-)?
答案 0 :(得分:1)
你会这样做:
macro_rules! f {
($in: ty) => {
match $in {
i32 => f32,
}
}
}
type OutputType = f!(i32);
其中 $ rustc typedef.rs
error: expected type, found keyword `match`
--> typedef.rs:3:5
|
3 | match $in {
| ^^^^^ expected type
...
9 | type OutputType = f!(i32);
| -------
| |
| this macro call doesn't expand to a type
| in this macro invocation
|
是局部变量。
如果你想咖喱,反过来做:
const favouriteList = useAppSelector((state) => selectFavouriteList(state, listKey));