user11081980 user11081980 - 11 days ago 7
TypeScript Question

TypeScript just for typings in Visual Studio 2015

I want to use TypeScript just for typings. I don't want to use classes, interfaces, or even use TypeScript definitions. The reason is that I'm using some jQuery plugins that don't have TypeScript definitions and I can't write them because of time constraints.

I keep reading that I should be able to use typings without the other features because TypeScript is based on JavaScript, but I really can't find a way. Consider the code below. I'm not using the jQuery type definition, therefore I get, "Cannot find name $". And again, I don't want to use TypeScript definitions if they don't cover everything I use.

So the TS file does not get transpiled into a JS file. And the build in Visual Studio 2015 fails.

My question is - how can I have this working in Visual Studio 2015? This is my TS file:

class Dto {
name: string;
age: number;
}

$.ajax({
url: "",
method: "PUT",
data: {}
})
.then(function (response: Dto) {
response.age = 16;
});

Answer

Use JQuery without typings

Before to use $, you can declare to the compiler it exists:

declare var $: any;

You can declare every global variables you use as of type any and the compiler won't complain about them.

Use the JQuery typings and a plugin without typings

With TypeScript, each interface can be completed from everywhere. After importing typings for JQuery, the missing members can be added from your own code:

interface JQuery {
  hereAMemberName: any
}
$('body').hereAMemberName // OK

And / or:

interface JQueryStatic {
  hereAMemberName: any
}
$.hereAMemberName // OK