Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用';新';使用TypeScript中的函数类型_Typescript - Fatal编程技术网

无法使用';新';使用TypeScript中的函数类型

无法使用';新';使用TypeScript中的函数类型,typescript,Typescript,我有以下代码: const BlockConstructors: Function[] = [ OBlock, IBlock, TBlock ]; function randomFromArray(array: any[]) { return array[Math.floor( Math.random() * array.length )]; } const BlockConstructor: Function = random(BlockConstructors); const

我有以下代码:

const BlockConstructors: Function[] = [
 OBlock,
 IBlock,
 TBlock
];

function randomFromArray(array: any[]) {
  return array[Math.floor( Math.random() * array.length )];
}

const BlockConstructor: Function = random(BlockConstructors);
const block: Block = new BlockConstructor();
我尝试从数组中绘制一个块构造函数,然后创建一个新对象,数组中的所有块构造函数都扩展了块类。我得到一个错误:

无法将“new”与类型缺少调用或构造签名的表达式一起使用


出现此错误的原因是什么?

您的代码不是自包含的,但下面是简要的原因

功能
不可
新增。在TypeScript中,只有三件东西可以是新的:

  • 具有构造签名的类型
  • 没有构造签名但具有返回
    void
  • any
  • 你真的想要第一个


    尝试从
    函数
    切换到
    (新建()=>块)

    常量应该是常量值,并且不应该对常量值进行新引用。为什么要创建一个无论如何都不会更改的新实例?:)