简体   繁体   English

我是否需要使用TypeScript在JQueryStatic定义文件中声明所有JQuery原型?

[英]Do I need to declare all my JQuery prototypes in a JQueryStatic definition file with TypeScript?

I have the following code: 我有以下代码:

///<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);
        }
    };
}

When I try to add the following: 当我尝试添加以下内容时:

  $('a').addTemplateSetup(

Into this same file I notice there is no intellisense and typescript does not seem to know about the addTemplateSetup prototype that I just added. 在这个文件中,我注意到没有智能感知和打字稿似乎不了解我刚刚添加的addTemplateSetup原型。

Is this the correct way for it to work or do I always need to add things like the definition for addTemplateSetup to an JQueryStatic definition file and then include that? 这是工作的正确方法吗?还是我总是需要将诸如addTemplateSetup的定义之类的内容添加到JQueryStatic定义文件中,然后将其包括在内?

You will need to declare it in some way to make Typescript not complain, the easiest is to declare it as any: 您将需要以某种方式声明它,以使Typescript不抱怨,最简单的方法是将其声明为any:

interface JQuery {
    addTemplateSetup: any;
}

But to get best intellisense support add info about the parameter types aswell: 但是,要获得最佳的智能感知支持,还请添加有关参数类型的信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript中的JQuery和JQueryStatic接口有什么区别? - What is the difference between the JQuery and the JQueryStatic interface in TypeScript? 为什么我的项目不需要 typescript 定义文件? - why my project do not need typescript definition files? 我是否必须在带有 typescript 的 React 中的可重用输入上声明占位符? - Do I have to declare placeholder on my reusable input in React with typescript? 构建真实世界的AngularJS应用程序,如何在html中声明我的控制器。 我是否需要在index.html中声明所有内容? - Building Real World AngularJS apps, how should I declare my controllers in html. Do I need to declare all in the index.html? 当我使用的TypeScript定义文件缺少单个定义时,我该怎么办? - What do I do when a TypeScript definition file I am using is missing a single definition? 我是否必须在每个文件中引用 TypeScript 定义? - Do I have to reference TypeScript definition in every file? 使用原型时,是否需要将所有内容包装在DOM负载上? - when working with prototypes do I need to wrap everything on DOM load? 我需要在javascript中声明一个对象吗? - Do I need to declare an object in javascript? 如何使jQuery在Typescript文件中工作? - How do I get jQuery to work in my Typescript files? .prop的JQuery typescript定义? - JQuery typescript definition for .prop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM