0

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?

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to import it in app.module.ts like this: "import '@types/jquery.cookie';" but when I compile code it throws error that couldn't find module.
Thanks for your answer! :)

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.