简体   繁体   English

打字稿:函数类型声明中的无效参数类型

[英]typescript: void parameter type in function type declaration

I'm starting my journey with ts and looking into TypeGraphQL right now.我正在开始我的ts之旅,现在正在研究TypeGraphQL I've noticed something I don't understand and haven't seen before:我注意到一些我不理解和以前从未见过的东西:

export declare type ReturnTypeFunc = (returns?: void) => ReturnTypeFuncValue;

How should that type be interpreted?应该如何解释该类型? What does this (returns?: void) represent?这个(returns?: void)代表什么?

The type ReturnTypeFunc is used in the Query decorator: ReturnTypeFunc类型在Query装饰器中使用:

export declare function Query(returnTypeFunc: ReturnTypeFunc, options?: AdvancedOptions): MethodDecorator;

This is how Query decorator is usually used: Query装饰器通常是这样使用的:

  @Query(returns => [SampleObject])

As far as I remember, the parameter returns is added to help with the readability: This query returns an array of SampleObject .据我所知,添加了returns参数以帮助提高可读性:此查询返回一个SampleObject数组。 If you remove the returns argument in type ReturnTypeFunc , you cannot do this anymore, and you have to write @Query(() => [SampleObject]) instead.如果删除类型ReturnTypeFunc中的returns参数,则不能再这样做,而必须改写@Query(() => [SampleObject])

Some other decorators that have an optional void argument are:其他一些具有可选 void 参数的装饰器是:

@Resolver(of => Recipe) // This is a resolver for Recipe. See type ClassTypeResolver

@Field(type => [Rate]) // This field is of type Rate. See type ClassTypeResolver.

Edit:编辑:

  1. As mentioned in the comments, you can only assign null (if strictNullChecks is not specified) or undefined to a variable of type void .如评论中所述,您只能将null (如果未指定strictNullChecks )或undefined分配给void类型的变量。
  2. The mentioned decorators annotate classes.提到的装饰器注释类。 The graphql schema is generated by using the provided metadata to the decorators. graphql 模式是通过使用提供给装饰器的元数据生成的。 To the best of my knowledge, you are not supposed to define a function that is of type ReturnTypeFunc .据我所知,您不应该定义类型为ReturnTypeFunc的函数。 The only reason those parameters are of type void , is to improve readability of code.这些参数是void类型的唯一原因是为了提高代码的可读性。

The function signature you described is here , and its usage of the optional parameter of type void could be written for a number of reasons.您描述的函数签名在这里,出于多种原因,可以编写void类型的可选参数的用法。

One reason is as a means of preventing callers from providing an argument in normal circumstances, but to allow for internal library code to make use of undocumented, internal logic that might depend on functional use of the value.一个原因是作为一种防止调用者在正常情况下提供参数的方法,但允许内部库代码使用可能依赖于值的功能使用的未记录的内部逻辑。

Here's a very contrived example that demonstrates what I described above:这是一个非常人为的例子,演示了我上面描述的内容:

TS Playground TS游乐场

function fn (param?: void): void {
  if ((param as any) === 'log a special message') {
    console.log('Internal behavior matched');
  }
};

fn(); // Ok
fn('hello'); // Not ok

/**
 * Written as returning void, but actually returns
 * something that is used for internal library behavior
 */
function internalFnForInteralThings (): void {
  return 'log a special message' as any;
};

fn(internalFnForInteralThings()); // Ok (and logs "Internal behavior matched")

Ultimately, you'd have to examine the whole codebase of the project that exports the type to see how it is used in each case, and then using that knowledge, you could infer why the author chose to write the signature that way.最终,您必须检查导出该类型的项目的整个代码库,以了解它在每种情况下的使用方式,然后利用这些知识,您可以推断出作者选择以这种方式编写签名的原因。

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

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