Typescript 减速器<&燃气轮机;Redux类型未将返回类型正确传递给减速器?

Typescript 减速器<&燃气轮机;Redux类型未将返回类型正确传递给减速器?,typescript,redux,Typescript,Redux,信息: 在Jetbrains Rider中使用Typescript 3.3.3333 给出了减速器的这种类型定义: 无需将AuthenticationState定义为返回值即可实现的行为 VS 如果减速器类型包含S的返回值,为什么不再对返回值进行类型检查? 任何光芒和智慧都是受欢迎的。提前感谢。

信息: 在Jetbrains Rider中使用Typescript 3.3.3333

给出了
减速器的这种类型定义:

无需将AuthenticationState定义为返回值即可实现的行为

VS

如果减速器类型包含S的返回值,为什么不再对返回值进行类型检查?


任何光芒和智慧都是受欢迎的。提前感谢。

:这是TypeScript中的预期行为,因为过度属性检查的工作方式

在您的例子中,您定义的arrow函数的返回类型具有多余(或额外)属性。要输入脚本,这完全可以

在一个简化的示例中,请查看此行为:

type a = () => { a: string };
type b = () => { a: string, b: string };

declare let myA: a;
declare let myB: b;

myA = myB; // OK! (excess properties are not checked)
myB = myA; // Error: missing required property 'b'
问题的关键在于,您实际上是在将
myB
分配给
myA
并期望出现错误,但只有在将
myA
分配给
myB
时才会出现错误

您有一些选项可以使代码按预期工作(例如您建议的选项,其中您明确定义了返回类型),但在TypeScript支持精确类型之前,这些选项都不是理想的(对于精确类型,我们讨论了您的精确用例w/redux和一些解决方法)

如上所述,这是预期的行为

如果您想拥有类型安全的redux体验, 然后,我建议您看一看一些redux扩展,它们的设计就是考虑到这一点的

退房


您的
tsc
版本是什么?我使用的是Jetbrains Rider的3.3.3333版本。请将编译器错误添加到您的问题中。非常感谢您了解这一点并与我们分享。非常感谢
export const INITIAL_STATE: AuthenticationState = {
    authenticationInProgress: false,
    accessToken: null,
    tokenType: null,
    expiryTimeMs: null,
    errorInformation: null,
    isAdmin: false,
};

export const authenticationReducer: Reducer<AuthenticationState, 
    AuthenticationActions> = (state = INITIAL_STATE, action): 
(why is this necessary??) ->  AuthenticationState => {
        switch (action.type) {
            case "authentication/user_authentication_request": {
                return {
                    ...state,
    problem -> hello: "hi",
                    authenticationInProgress: true,
                };
            }
...
type a = () => { a: string };
type b = () => { a: string, b: string };

declare let myA: a;
declare let myB: b;

myA = myB; // OK! (excess properties are not checked)
myB = myA; // Error: missing required property 'b'