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,在Typescript中,为什么允许这样做 const emptyObject: {} = "test"; 当我写了一个类似这样的函数时,我偶然发现了它: function test(input: boolean) { if (input) return 'test'; else return {}; } 并且惊讶地看到函数的类型签名是函数测试(输入:布尔):{},而不是预期的函数测试(输入:布尔):{}|“测试”

在Typescript中,为什么允许这样做

const emptyObject: {} = "test";
当我写了一个类似这样的函数时,我偶然发现了它:

function test(input: boolean) {
  if (input) return 'test';
  else return {};
}
并且惊讶地看到函数的类型签名是
函数测试(输入:布尔):{}
,而不是预期的
函数测试(输入:布尔):{}|“测试”