简体   繁体   中英

Typescript Union of function with other type throws error

I'm trying to have a property with a union type of a function and a string.

export type IMenuItem = {
    target: string | (val1:any[], val2:number) => number;
}

TS playground sample can be accessed here .

But the TS compiler gives an error: "Function type notation must be parenthesized when used in a union type."

Is the type declared incorrectly? Or is there a workaround for this?

However, it works if we remove the union.

export type IMenuItem = {
    target: (val1:any[], val2:number) => number;
}

You need to wrap you function with parenthesizes

export type IMenuItem = {
    target: string | ((val1:any[], val2:number) => number);
}

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