【问题标题】:can the type specified after the colons in typescript be a function?typescript中冒号后指定的类型可以是函数吗?
【发布时间】:2021-11-24 18:34:03
【问题描述】:

我在 typeScript 中的 module.d.ts 中遇到过这个导出声明:

export declare const extend: (objects: object) => void

是“const extend”类型的对象,为什么它返回void而不是指定的类型?

【问题讨论】:

标签: typescript


【解决方案1】:

export declare const extend 表示:

常量extend 已被声明,它有一个函数类型:(objects: object) => void

因此,extend 是一个函数。

以上代码等于(declared 关键字除外):

export const extend=(objects: object) => { 
   /* function implementation which returns nothing */ 
}

【讨论】:

  • 啊谢谢我很困惑,因为我认为它会是这样的:export declare const extend: funtion; extend = function():void{ /*函数实现*/ }
  • 我不确定你想要实现什么,但是这个语法:const extend: funtion; extend = function():void{ /*function implmentaiton*/ } 是无效的。
  • 只是我在 react-three-fiber 包中发现了这个,它显然用于导出模块,如: module sth: ```` classes sth { /* class body*/ } extend ( {某事}); ```` 扩展中的函数不应该返回传递的对象吗?无论如何,没关系,我想我必须广泛学习打字稿。谢谢
  • @AbdulsalamAlmahdi 我建议你从TS手册开始typescriptlang.org/docs/handbook/intro.html
  • function是将函数定义为JS代码的关键字。 () => T 是声明函数 type 的语法 :)
猜你喜欢
  • 1970-01-01
  • 2020-08-24
  • 2012-09-28
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多