nick_green
nick_green

Reputation: 1664

angular2 typescript $.cookie() is not a function

I installed @types/[email protected], then in tsconfig.json I added jquery.cookie in types section. Visual Studio Code shows that $.cookie is available to use, but when I run my code I get error in console that $.cookie() is not a function. Where is the problem? Am I missing something? Should I reference it somewhere else?

Upvotes: 0

Views: 760

Answers (1)

fredrik
fredrik

Reputation: 17617

Have you included the jquery.cookie package in your code? Or just the @types/[email protected]?

@types are just definition files for TypeScript, not the actual code it self. So we still need to install the code it self:

npm install --save jquery.cookie

Then add it to your packaging, for instance for SystemJS:

SystemJS.config({
    'map': {
        'jquery.cookie': 'npm:jquery.cookie'
    },
    'paths': {
        'npm:': 'node_modules/'
    }
});

To sum up:

  • @types are definitions to allow TypeScript understand code/packages written in JavaScript (as most of them are or at least compiled to). @types shouldn't be imported in the code it self. Only installed, the TypeScript compiler automatically looks for all defintions in node_modules/@types.
  • The package you want to use with TypeScript still needs to be installed with npm (or yarn). These hold the actual code.

Upvotes: 1

Related Questions