javascript - 使用: and => for the return type with a TypeScript function?有什么区别

标签 javascript jquery typescript

我有以下代码:

///<reference path="../typescript/jquery.d.ts" />
function addThemePrototypes() {
    var templateSetup = new Array();
    $.fn.addTemplateSetup = function(func, prioritary)
    {
        if (prioritary)
        {
            templateSetup.unshift(func);
        }
        else
        {
            templateSetup.push(func);
        }
    };
}

有人能告诉我为什么要用 => void 来声明吗?

interface JQuery {
    addTemplateSetup: (func: Function, priority: bool) =>void;
}

我想我对如何从 javascript 函数执行 returntype 有点困惑。有时候我 请参阅:jQuery,现在我正在使用 => void。两者有什么区别?

最佳答案

在您的示例中,它们都使用冒号声明...

让我们把它分成两半!

没有类型声明...

interface JQuery {
    addTemplateSetup
}

类型声明...

: (func: Function, priority: bool) =>void;

如果你用 is 这个词代替 : 就像在说

The property named 'addTemplateSetUp' : A function accepting two parameters of specific types and not returning a value

这是一个示例,其中包含两个实际上相同的接口(interface),但一个使用 => 而另一个不使用。 TypeScript 对两者的处理方式相同,因此这实际上取决于开发人员的偏好。

interface JQuery {
    addTemplateSetup: (func: Function, priority: bool) =>void;
}

interface Identical {
    addTemplateSetup(func: Function, priority: bool): void;
}

class ImplementsEither implements JQuery {
    addTemplateSetup (func: Function, priority: bool) : void {

    }
}

如果您喜欢几乎用英文阅读,您可以使用 => 样式声明。

如果您希望您的界面看起来更像实现,您可以使用第二种风格。

关于javascript - 使用: and => for the return type with a TypeScript function?有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173001/

相关文章:

javascript - 将字符串拆分为等长字符串数组

javascript - JS - 如何将函数作为参数(带参数)传递而不调用它

javascript - 我可以从 img 标签将图像上传到 firebase 存储吗

javascript - 单击 div 和带有 JS 的按钮后重定向用户

javascript - Google AdWords 转换服务问题 - 异步转换代码

node.js - 如何启动 Node 以从 IntelliJ 运行 .ts 文件?

javascript - 如何在不重新启动服务器的情况下更改 Javascript?

javascript - Bootstrap的文档子导航怎么做?

typescript - 使用 Vue 3 + vue-class-component + TypeScript 的 Vuelidate

Typescript:根据对象中子键的存在创建联合类型