Fixing linq typings to work with es6

I’m sure that everyone knows linq. It is library which is very useful and has version not only in C# but also in JavaScript. Using linq in javascript may be useless few years ago but now it’s common problem that we need do some buisiness logic operations in js. I worked on few projects where i used Linq in javascript but lately i wanted to use it in Aurelia which is using es6 (ecmascript 6). First i had a problem how to use npm library in Aurelia which use jspm. I asked about that in stackoverflow and get the answer. But then i started using typescript and everything f**ed up.

First i needed typings for linq, so i googled linq.d.ts and get first answer. I added this to my project and created first typescript class. What happened? Visual studio shows error that cannot find module ‚linq’.

I thought that this is something with npm package. But when i wrote the same import in javascript it worked fine. So i checked linq.d.ts file and tried to changed it to meet the import statement. I didn’t changed any interfaces to be sure that i will have correct typings, but what i changed was module declaration. Original module declaration was:

declare module linq

I changed that to:

declare module 'linq'

Error about module disappear but i got another error.

So i had to move Enumerable variable into module declaration and make this variable default export.

declare module 'linq' {

    var Enumerable: EnumerableStatic;
    export default Enumerable;

That helped, there is no error anymore. It should work for most of you, but because of my clumsiness i didn’t noticed that linq in some version changed their functions names. In some older version it was PascalCase (with first letter is capital ex. SingleOrDefault()) and now it is the same as all javascript functions so camelCase (ex. singleOrDefault()). So again i had to change my typings. I found the newest one at linqjs codeplex page. Did the same changes (with module name and default export) again and now i can use linq in typescript with ecmascript 6 features.

If anyone interested here you can download linq.d.ts file.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

This site uses Akismet to reduce spam. Learn how your comment data is processed.