1

I am trying to write an interface to describe the foundation JQuery plugin. The plugin uses the first parameter to determine the child plugin that is called with the foundation plugin.

Below is an example of what i am trying to achieve.

/// <reference path="jquery.d.ts"/>

interface JQuery {
  foundation(plugin:'reveal',action:string):void;
  foundation(plugin:'dropdown',action:string,$el:JQuery):void;
}

How do i describe a fixed string parameter within a typescript definition?

1 Answer 1

2

You need to provide a non-specialized function signature. However this does open up possibilities of error:

interface JQuery {
  foundation(plugin:'reveal',action:string):void;
  foundation(plugin:'dropdown',action:string,$el:JQuery):void;
  foundation(plugin:string,action:string,$el?:JQuery):void;
}

// SADLY 
var foo:JQuery;
foo.foundation('sad','panda');

WHY

The reason is that there is no deterministic way for a compiler to always know ahead of time if a particular string is going to have an exact value. This string might be coming from userinput/database. The idea here is to bring this information directly to your notice.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought there may be some sneaky way to do this. Its such a pain especially dealing with many JQuery plugins that seem to operate in a similar way.
Makes sense, don't know why I did not see it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.