简体   繁体   English

在TypeScript中使用可选参数调用函数

[英]Calling function with optional argument in TypeScript

I'm trying to patch graphql-cli 's get-schema function to print description using comment. 我正在尝试修补graphql-cliget-schema函数以使用注释打印描述。 I forked the library [ https://github.com/poernahi/graphql-cli] and edited src/cmds/get-schema.ts : 我分叉了库[ https://github.com/poernahi/graphql-cli]并编辑了src/cmds/get-schema.ts

printSchema(newSchemaResult as GraphQLSchema)

becomes

printSchema(newSchemaResult as GraphQLSchema, {commentDescriptions: true})

When I run npm install , tsc did not like my meddling.. 当我运行npm installtsc不喜欢我的插手..

src/cmds/get-schema.ts(194,11): error TS2554: Expected 1 arguments, but got 2.

Now I really don't understand this error, because when I look at node_modules/graphql/utilities/schemaPrinter.js.flow , I see the function signature clearly indicates a second optional parameter. 现在我真的不明白这个错误,因为当我查看node_modules/graphql/utilities/schemaPrinter.js.flow ,我看到函数签名清楚地表明了第二个可选参数。

type Options = {| commentDescriptions?: boolean |};

export function printSchema(schema: GraphQLSchema, options?: Options): string

I tried using undefined as the second parameter and still get the same error. 我尝试使用undefined作为第二个参数仍然得到相同的错误。 I read the ts documentation and this is the right syntax to define optional param, even if it is written in flow. 我阅读了ts文档,这是定义可选参数的正确语法,即使它是用流写的。

Is this caused by mixing flow and typescript? 这是由混合流和打字稿引起的吗?

How did typescript get the argument list? 打字稿是如何获得参数列表的? I walked through the import chain and it pointed to the definition above. 我走过了进口链,它指出了上面的定义。

What am I missing? 我错过了什么? Any workaround? 任何解决方法?

Thanks 谢谢

What you are looking at are the flow type annotations. 您正在查看的是流类型注释。 They have nothing to do, with what TypeScript picks up for the typings of the library. 它们无关,TypeScript选择了库的类型。 In the case of graphql the typings actually come from a community hosted typing repository which you can find here: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/graphql graphql的情况下, graphql实际上来自社区托管的输入存储库,你可以在这里找到: https//github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/graphql

When you take a look at the typing for the function you want to use, you can see, that the second argument is not added and therefore TypeScript doesn't know anything about it: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/graphql/utilities/schemaPrinter.d.ts#L4 当您查看要使用的函数的类型时,您可以看到第二个参数未添加,因此TypeScript对此一无所知: https//github.com/DefinitelyTyped/DefinitelyTyped/斑点/主/类型/ graphql /公用事业/ schemaPrinter.d.ts#L4

So in order for TypeScript to properly know the second argument, you have to add it to the typings file over at DefinitelyTyped. 因此,为了让TypeScript正确地知道第二个参数,您必须将它添加到DefinitelyTyped的typings文件中。

The TypeScript types do not specify an options parameter: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/graphql/utilities/schemaPrinter.d.ts#L4 TypeScript类型不指定options参数: https//github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/graphql/utilities/schemaPrinter.d.ts#L4

So it looks like there is a discrepancy between Flow and TS as you suspected. 所以看起来你怀疑Flow与TS之间存在差异。

You could look into overriding the TS typings manually using declaration merging to define your own signature for the printSchema method. 您可以使用声明合并来手动覆盖TS类型,以便为printSchema方法定义自己的签名。 You have to be sure the printSchema method really accepts the second options argument though. 您必须确保printSchema方法确实接受第二个options参数。 Perhaps the flow typings are out of date? 也许流量类型已经过时了?

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

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