简体   繁体   English

我可以在Typescript定义和函数调用中将参数设为可选参数吗?

[英]Can I make a parameter optional in a Typescript definition and function call?

Here is my call: 这是我的电话:

this.enumService.getData('ContentStatus'),

It's giving an error as my definition is: 由于我的定义是错误的:

getData(controller: any, params: any);

The function looks like this: 该函数如下所示:

getData = (controller, params) => {
    if (!params) { params = "" }
    var defer = this.$q.defer();

How can I fix the definition and the function so a null is allowed? 如何修复定义和函数,以便允许使用null?

For your case the declaration ? 对于您的情况,声明? :

getData(controller: any, params?: any);

And if you are defining in TypeScript you can use default parameters = : 如果您在TypeScript中定义,则可以使用默认参数=

getData = (controller, params = "") => {
    var defer = this.$q.defer();

The documentation can be found on the typescript following construction function create(name: string, animalOptions?: AnimalOptions): Animal; 该文档可以在构造function create(name: string, animalOptions?: AnimalOptions): Animal;的打字稿上找到function create(name: string, animalOptions?: AnimalOptions): Animal; use "?" 采用 ”?”

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

相关问题 如何在打字稿中使用默认和可选参数调用函数 - How can i call function with default and optional parameter in typescript 我可以在Typescript中重用函数的参数定义吗? - Can I reuse the parameter definition of a function in Typescript? 如何在打字稿中为函数的可选参数设置默认值? - How can I set a default value for an optional parameter of a function in typescript? 如何将可选的命名参数添加到TypeScript函数参数? - How can I add optional named parameters to a TypeScript function parameter? 如何有条件地为 TypeScript 中的 function 提供可选参数? - How can I conditionally make an argument optional for a function in TypeScript? 如何使 TypeScript 解构 function 参数,分配默认值,以及可选的整个 object 类型? - How can I make TypeScript deconstruct function parameter, assign default value, and optional whole object type at same time? 根据 Typescript 中的另一个函数参数使函数参数成为必需/可选 - Make a function parameter required/optional based on another function parameter in Typescript 如何使 function 参数尊重 TypeScript 中的 object 类型? - How can I make a function parameter respect an object type in TypeScript? Typescript:输入带有可选参数的 function - Typescript: Type a function with optional parameter 打字稿-带有可选参数的通用函数 - Typescript - Generic function with an optional parameter
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM