Adr*_*scu 14 typescript reactjs redux-thunk
我有一个redux thunk动作,它获取一些数据然后调度一些动作(这里没有显示代码,但是你可以在下面的demo链接中找到它)
export const fetchPosts = (id: string) => (dispatch: Dispatch<TActions>) => {
return fetch('http://example.com').then(
response => {
return response.json().then(json => {
return "Success message";
});
},
err => {
throw err;
}
);
};
Run Code Online (Sandbox Code Playgroud)
而不是在我的组件我用mapDispatchToProps用bindActionCreators,从我的组件调用该函数如下所示:
public fetchFunc() {
this.props.fetchPosts("test").then(
res => {
console.log("Res from app", res);
},
err => {
console.log("Err from app", err);
}
);
}
Run Code Online (Sandbox Code Playgroud)
由于我使用的是typescript,我需要在Props中定义这个函数的类型
interface IProps {
name?: string;
posts: IPost[];
loading: boolean;
fetchPosts: (id: string) => Promise<string | Error>;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做 - 就像上面那样,Typescript会抱怨我应该这样做 - 就像这样:
fetchPosts: (id: string) => (dispatch: Dispatch<TActions>) => Promise<string | Error>;
Run Code Online (Sandbox Code Playgroud)
如果我这样做的话,那么当我then在我的组件中使用时,Typescript会抱怨说该函数不是一个承诺.
我创建了一个演示程序,您可以在其中编写代码
按"从远程加载"有时会失败,看看是否承诺:
问题是呼叫bindActionCreators在mapDispatchToProps.在运行时bindActionCreators基本上将其(id: string) => (dispatch: Dispatch<TActions>) => Promise<string>;转换为此(id: string) => Promise<string>;,但类型bindActionCreators不反映此转换.这可能是因为要实现这一目标,您需要条件类型,直到最近才可用.
如果我们从redux repo中查看这个示例用法,我们会看到它们通过明确指定函数的类型来完成转换:
const boundAddTodoViaThunk = bindActionCreators<
ActionCreator<AddTodoThunk>,
ActionCreator<AddTodoAction>
>(addTodoViaThunk, dispatch)
Run Code Online (Sandbox Code Playgroud)
我们可以在您的代码中执行相同的操作,引用现有类型,但这会损害类型安全性,因为没有检查这fetchPosts两种类型是否会正确键入:
const mapDispatchToProps = (dispatch: Dispatch<TActions>): Partial<IProps> =>
bindActionCreators<{ fetchPosts: typeof fetchPosts }, Pick<IProps, 'fetchPosts'>>(
{
fetchPosts
},
dispatch
);
Run Code Online (Sandbox Code Playgroud)
或者我们可以使用类型断言,因为上述方法无论如何都不提供任何安全性:
const mapDispatchToProps2 = (dispatch: Dispatch<TActions>) =>
bindActionCreators({
fetchPosts: fetchPosts as any as ((id: string) => Promise<string>)
}, dispatch );
Run Code Online (Sandbox Code Playgroud)
对于真正类型安全的方法,我们需要使用typescript 2.8和带辅助函数的条件类型.我们可以输入bindActionCreators它应该的方式,并自动推断生成的创建者的正确类型:
function mybindActionCreators<M extends ActionCreatorsMapObject>(map: M, dispatch: Dispatch<TActions>) {
return bindActionCreators<M, { [P in keyof M] : RemoveDispatch<M[P]> }>(map, dispatch);
}
const mapDispatchToProps = (dispatch: Dispatch<TActions>) =>
mybindActionCreators(
{
fetchPosts
},
dispatch
);
// Helpers
type IsValidArg<T> = T extends object ? keyof T extends never ? false : true : true;
type RemoveDispatch<T extends Function> =
T extends (a: infer A, b: infer B, c: infer C, d: infer D, e: infer E, f: infer F, g: infer G, h: infer H, i: infer I, j: infer J) => (dispatch: Dispatch<any>) => infer R ? (
IsValidArg<J> extends true ? (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => R :
IsValidArg<I> extends true ? (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => R :
IsValidArg<H> extends true ? (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => R :
IsValidArg<G> extends true ? (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => R :
IsValidArg<F> extends true ? (a: A, b: B, c: C, d: D, e: E, f: F) => R :
IsValidArg<E> extends true ? (a: A, b: B, c: C, d: D, e: E) => R :
IsValidArg<D> extends true ? (a: A, b: B, c: C, d: D) => R :
IsValidArg<C> extends true ? (a: A, b: B, c: C) => R :
IsValidArg<B> extends true ? (a: A, b: B) => R :
IsValidArg<A> extends true ? (a: A) => R :
() => R
) : T;
Run Code Online (Sandbox Code Playgroud)
谢谢@Thai Duong Tran 和@Titian Cernicova-Dragomir。
我发现您提供的两个答案之间存在混合。
1:
在 props 中,我可以说该函数具有原始函数的类型,而不是重新声明所有参数类型和返回类型:(fetchPosts: typeof fetchPosts感谢@titian-cernicova-dragomir)
2:
现在我可以使用该功能,但不能作为承诺。为了使这一承诺成为可能,我可以使用 @thai-duong-tran 提供的解决方案。
const fetchPromise = new Promise(resolve => {
this.props.fetchPosts("adidas");
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看工作演示:https://codesandbox.io/s/zo15pj633
| 归档时间: |
|
| 查看次数: |
2208 次 |
| 最近记录: |