简体   繁体   English

Typescript - 接口中的函数返回类型

[英]Typescript - function return type in interface

Trying to do this试图做到这一点

interface SomeInterface {
    someProperty: string | () => JSX.Element
}

The property should be either a string or a Function that returns a JSX.Element .该属性应该是返回JSX.ElementstringFunction What's the proper syntax, if any?如果有的话,正确的语法是什么?

Currently I'm doing the following as a better-than-nothing solution, but it's not what I want目前我正在做以下作为一个比没有更好的解决方案,但这不是我想要的

interface SomeInterface {
    someProperty: string | Function
}

Yup this is correct with parenthesis :是的,括号是正确的:

interface SomeInterface {
    someProperty: string | (() => JSX.Element)
}


declare const s: SomeInterface

if (typeof s.someProperty === 'function') {
    s.someProperty() // JSX.Element
} else {
    s.someProperty // string
}

Playground 操场

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

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