什么编程语言有冒号"inside"和"after"函数的参数

What programming language has a colon "inside" and "after" the parameter of a function

我遇到了这个函数,我一直在寻找具有这种语法的编程语言:

function getDiscount(items: Item[]): number {

   if(items.length === 0){
      throw new Error("Items cannot be empty");
   }

   let discount = 0;

   items.forEach(function(item) {
      discount += item.discount;
   })

   return discount/100;}

参数由冒号(:)分隔,然后参数后跟另一个冒号。我尝试 运行 控制台上的代码,但出现错误“Uncaught SyntaxError: Unexpected token ':'”

我能找到的最接近的是 Python 的函数注释,但是,参数后跟的是箭头而不是冒号。

我也想知道第一行的代码是什么意思——参数以及参数后面的内容。我的理解是,将要传递的参数将被插入到一个数组中,而返回的数据类型是一个数字。如有不妥请指正

此代码是用 TypeScript (https://www.typescriptlang.org) 编写的,它是 JavaScript 的超集。 TypeScript 代码对添加的类型有效 JavaScript。
实际上,如果您删除了类型注释,您可以在浏览器控制台中 运行 此代码:

function getDiscount(items) {

   if(items.length === 0){
      throw new Error("Items cannot be empty");
   }

   let discount = 0;

   items.forEach(function(item) {
      discount += item.discount;
   })

   return discount/100;
}

TypeScript 需要转换成 JavaScript 才能在浏览器或 NodeJS 中使用。转换过程称为 'transpiling',它类似于编译,但更像是从一种人类可读语言到另一种语言的转换,而不是典型的从人类可读语言到机器可读语言的转换。 (我根据 Caius Jard 的建议更新了此描述)

函数定义中的类型注释意味着它采用类型为 Item 的项目数组作为参数,returns 类型为 number。 从代码中我们可以说类型 Item 是一个对象,它至少有一个键 discount,类型为 number。此代码将迭代 Item 的数组和 return 所有折扣的总和。