简体   繁体   中英

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?

For your case the declaration ? :

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

And if you are defining in TypeScript you can use default parameters = :

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; use "?"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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