简体   繁体   English

如何定义一个在打字稿中无法定义的变量?

[英]How to define a variable that can be undefined in typescript?

I am using typesafe-actions and I have the following declaration: 我正在使用typesafe-actions,并且有以下声明:

function getAction<Payload>():PayloadAC<string, Payload>  {
    const p = createStandardAction('FETCH')<Payload>();
    return p; // <------- Here is get typescript error
}

(This method is only an example that clarify my question, it is not a real function in my code) (此方法只是一个阐明我的问题的示例,它不是我的代码中的真实函数)
This code produce typescript error: 这段代码产生打字稿错误:

Type '[Payload] extends [undefined] ? unknown extends Payload ? PayloadAC<"FETCH", Payload> : EmptyAC<"FETCH"> : PayloadAC<"FETCH", Payload>' is not assignable to type 'PayloadAC<string, Payload>'.
  Type '(unknown extends Payload ? PayloadAC<"FETCH", Payload> : EmptyAC<"FETCH">) | PayloadAC<"FETCH", Payload>' is not assignable to type 'PayloadAC<string, Payload>'.
    Type 'unknown extends Payload ? PayloadAC<"FETCH", Payload> : EmptyAC<"FETCH">' is not assignable to type 'PayloadAC<string, Payload>'.
      Type 'PayloadAC<"FETCH", Payload> | EmptyAC<"FETCH">' is not assignable to type 'PayloadAC<string, Payload>'.
        Type 'EmptyAC<"FETCH">' is not assignable to type 'PayloadAC<string, Payload>'.
          Property 'payload' is missing in type 'EmptyAction<"FETCH">' but required in type 'PayloadAction<string, Payload>'.ts(2322)
type-helpers.d.ts(45, 5): 'payload' is declared here.

Forget the fact that the error is very difficult to understand. 忘记错误很难理解的事实。 Eventually I managed to understand that createStandardAction('FETCH')<Payload>() returns PayloadAC<"FETCH", Payload> or EmptyAC<"FETCH"> according to the checks [Payload] extends [undefined] ? unknown extends Payload ... 最终,我根据[Payload] extends [undefined] ? unknown extends Payload ...的检查结果,成功理解createStandardAction('FETCH')<Payload>()返回PayloadAC<"FETCH", Payload>EmptyAC<"FETCH"> [Payload] extends [undefined] ? unknown extends Payload ... [Payload] extends [undefined] ? unknown extends Payload ... . [Payload] extends [undefined] ? unknown extends Payload ...
The thing is that I don't really care wether Payload is undefined or not. 事实是,我真的不在乎有效载荷是否未定义。 I am fine with setting payload as undefined: {type: 'FETCH', payload: undefined } . 我可以将有效负载设置为undefined: {type: 'FETCH', payload: undefined } But I don't know how to declare that in typescript. 但是我不知道如何在打字稿中声明它。
This forces me to define the return value of the function as PayloadAC<string, Payload> | EmptyAC<string> 这迫使我将函数的返回值定义为PayloadAC<string, Payload> | EmptyAC<string> PayloadAC<string, Payload> | EmptyAC<string> which causes me other problems, for example: PayloadAC<string, Payload> | EmptyAC<string>这会导致我其他问题,例如:

const action = getAction<MyType>();
const { payload } = action; // <--- Here I get an exception that payload may not exists

In this example I don't really mind to get payload as undefined but typescript is yelling at me. 在这个例子中,我真的不介意获得未定义的有效负载,但是打字稿对我大吼大叫。

How do I fix this mess? 如何解决此问题?

Since you don't care if payload is undefined it sounds like the return type is indeed PayloadAC<string, Payload> | EmptyAC<string> 由于您不在乎有效载荷是否未定义,因此听起来像返回类型的确是PayloadAC<string, Payload> | EmptyAC<string> PayloadAC<string, Payload> | EmptyAC<string> . PayloadAC<string, Payload> | EmptyAC<string> Yes, you will get errors if you then try and access the payload. 是的,如果您尝试访问有效负载,则会收到错误消息。 This makes it sound like you actually DO care if payload is undefined. 这听起来好像您实际上在乎有效载荷是否未定义。

Also, there is no reason const { payload } = action; 同样,没有理由const { payload } = action; should give you an error if payload is undefined. 如果未定义有效负载,应该会给您一个错误。 Example here . 这里的例子 So maybe you are doing something else with payload where a value is expected? 因此,也许您正在使用有效载荷来做其他事情,而期望值是期望的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM