【发布时间】:2019-12-17 23:07:30
【问题描述】:
使用 TypeScript,您可以显示一个变量实现了一个接口:
type Component<P = { children: string | number }> = (
props: P
) => string | number;
const Children1: Component = props => props.children;
导致:
const Children1 = props => props.children;
是否可以使用常规功能实现类似的功能?其输出将类似于:
// Where does the type information go?
function Children2(props) {
return props.children;
}
你可以在TypeScript Playground上玩这个例子
(至于为什么,常规函数是有用的,因为你可以提升它们)
【问题讨论】:
-
只要使用
const name: type = function () {} -
在您的第一个示例中,无论您是否输入,转换后的输出都是相同的。键入函数不是为转译器,而是为开发人员,所以你的智能感知更好
-
@jonrsharpe 在您的示例中,输出与第一个示例相同,这意味着您不能以提升方式使用它,例如在文件底部声明函数并在顶部使用它们。
-
但是为什么输入的参数类型和返回类型不够?
标签: typescript